You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

misc.cpp 3.8 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * \file imperative/src/impl/ops/tensor_manip.cpp
  3. * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  4. *
  5. * Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
  6. *
  7. * Unless required by applicable law or agreed to in writing, software
  8. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  9. * ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. */
  11. #include "../op_trait.h"
  12. #include "megbrain/imperative/ops/autogen.h"
  13. #include "megbrain/opr/misc.h"
  14. namespace mgb {
  15. namespace imperative {
  16. namespace check_non_finite {
  17. SymbolVarArray apply_on_var_node(const OpDef& def, const VarNodeArray& inputs) {
  18. auto&& op = def.cast_final_safe<CheckNonFinite>();
  19. OperatorNodeConfig config{op.make_name()};
  20. return opr::CheckNonFinite::make(inputs, op.param(), config);
  21. }
  22. SmallVector<TensorPtr> apply_on_physical_tensor(
  23. const OpDef& def, const SmallVector<TensorPtr>& inputs) {
  24. size_t size = inputs.size();
  25. auto&& op = def.cast_final_safe<CheckNonFinite>();
  26. SmallVector<TensorPtr> outputs(size + 1);
  27. outputs[size] = Tensor::make(
  28. TensorLayout(TensorShape({1}), dtype::Int32()), inputs[0]->comp_node());
  29. auto dest = outputs[size];
  30. auto cn = dest->comp_node();
  31. auto&& dnn_opr = opr::intl::create_megdnn_opr<megdnn::CheckNonFinite>(cn);
  32. size_t wk_size = 0;
  33. SmallVector<megdnn::TensorND> srcs(size);
  34. // copy an outputs to the dnn for inplace
  35. for (size_t i = 0; i < size; ++i) {
  36. outputs[i] = Tensor::make(inputs[i]->layout(), inputs[0]->comp_node());
  37. outputs[i]->dev_tensor().copy_from_fixlayout(inputs[i]->dev_tensor());
  38. srcs[i] = outputs[i]->dev_tensor().as_megdnn();
  39. }
  40. megdnn::CheckNonFinite::Param param({op.scale});
  41. dnn_opr->param() = param;
  42. wk_size = dnn_opr->get_workspace_in_bytes(srcs, dest->layout());
  43. auto wk = Blob::make(cn, wk_size);
  44. megdnn::Workspace dnn_wk(wk->storage().get(), wk_size);
  45. dnn_opr->exec(srcs, dest->dev_tensor().as_megdnn(), dnn_wk);
  46. return outputs;
  47. }
  48. std::tuple<SmallVector<LogicalTensorDesc>, bool> infer_output_attrs_fallible(
  49. const OpDef& def, const SmallVector<LogicalTensorDesc>& inputs) {
  50. size_t size = inputs.size();
  51. SmallVector<LogicalTensorDesc> dests(size + 1);
  52. for (size_t i = 0; i < size; ++i) {
  53. dests[i].comp_node = inputs[i].comp_node;
  54. dests[i].layout = inputs[i].layout;
  55. }
  56. dests[size].comp_node = inputs[0].comp_node;
  57. dests[size].layout = TensorLayout(TensorShape({1}), dtype::Int32());
  58. return {dests, true};
  59. }
  60. SmallVector<LogicalTensorDesc> infer_output_attrs(
  61. const OpDef& def, const SmallVector<TensorPtr>& inputs) {
  62. size_t size = inputs.size();
  63. SmallVector<LogicalTensorDesc> dests(size + 1);
  64. for (size_t i = 0; i < size; ++i) {
  65. dests[i].comp_node = inputs[i]->comp_node();
  66. dests[i].layout = inputs[i]->layout();
  67. }
  68. dests[size].comp_node = inputs[0]->comp_node();
  69. dests[size].layout = TensorLayout(TensorShape({1}), dtype::Int32());
  70. return dests;
  71. }
  72. std::tuple<SmallVector<MemoryDesc>, SmallVector<MemoryDesc>> infer_output_mem_desc(
  73. const OpDef& def, const SmallVector<TensorPtr>& inputs_tensors,
  74. const SmallVector<MemoryDesc>& inputs_mems) {
  75. return {{}, {}};
  76. }
  77. OP_TRAIT_REG(CheckNonFinite, CheckNonFinite)
  78. .apply_on_var_node(apply_on_var_node)
  79. .apply_on_physical_tensor(apply_on_physical_tensor)
  80. .infer_output_attrs_fallible(infer_output_attrs_fallible)
  81. .infer_output_mem_desc(infer_output_mem_desc)
  82. .fallback();
  83. } // namespace check_non_finite
  84. } // namespace imperative
  85. } // namespace mgb
  86. // vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}}