diff --git a/mindspore/ccsrc/backend/kernel_compiler/aicpu/aicpu_kernel_build.cc b/mindspore/ccsrc/backend/kernel_compiler/aicpu/aicpu_kernel_build.cc index 5cb0d411e50..78f1c2ff375 100644 --- a/mindspore/ccsrc/backend/kernel_compiler/aicpu/aicpu_kernel_build.cc +++ b/mindspore/ccsrc/backend/kernel_compiler/aicpu/aicpu_kernel_build.cc @@ -108,7 +108,7 @@ void ParseAttrValue(const std::string &type, const std::string &attr_name, const MS_EXCEPTION_IF_NULL(node_attr); MS_EXCEPTION_IF_NULL(value); if (type == "int") { - auto attr_value = static_cast(GetValue(value)); + auto attr_value = value->isa() ? GetValue(value) : GetValue(value); (*node_attr)[attr_name].set_i(attr_value); } else if (type == "str") { auto attr_value = GetValue(value); @@ -186,6 +186,12 @@ void SetNodeInputs(const std::shared_ptr &anf_node, mindspore::NodeDef return; } + std::vector input_size_list; + if (!SetIOIputSize(anf_node, input_num, &input_size_list)) { + MS_LOG(ERROR) << "Node [" << AnfAlgo::GetCNodeName(anf_node) << "] get input size list failed."; + return; + } + for (size_t input_index = 0; input_index < input_num; input_index++) { ::mindspore::Tensor *node_inputs = proto->add_inputs(); MS_EXCEPTION_IF_NULL(node_inputs); @@ -215,6 +221,7 @@ void SetNodeInputs(const std::shared_ptr &anf_node, mindspore::NodeDef } node_inputs->set_tensor_type(input_data_type); node_inputs->set_mem_device("HBM"); + node_inputs->set_data_size(input_size_list[input_index]); } } @@ -243,8 +250,17 @@ void SetNodeOutputs(const std::shared_ptr &anf_node, mindspore::NodeDef } TypeId output_type = AnfAlgo::GetOutputDeviceDataType(anf_node, output_index); int32_t output_data_type = AicpuOpUtil::MsTypeToProtoType(output_type); + + int64_t data_size = 1; + if (!GetShapeSize(output_shape, TypeIdToType(output_type), &data_size)) { + MS_LOG(ERROR) << "Node [" << AnfAlgo::GetCNodeName(anf_node) << "] get output size failed for output " + << output_index; + return; + } + node_outputs->set_tensor_type(output_data_type); node_outputs->set_mem_device("HBM"); + node_outputs->set_data_size(LongToSize(data_size)); } } diff --git a/mindspore/ccsrc/backend/kernel_compiler/aicpu/aicpu_ops/CMakeLists.txt b/mindspore/ccsrc/backend/kernel_compiler/aicpu/aicpu_ops/CMakeLists.txt index a09149cb7e1..01cdb6e70e3 100644 --- a/mindspore/ccsrc/backend/kernel_compiler/aicpu/aicpu_ops/CMakeLists.txt +++ b/mindspore/ccsrc/backend/kernel_compiler/aicpu/aicpu_ops/CMakeLists.txt @@ -25,6 +25,11 @@ if(EXISTS ${CMAKE_C_COMPILER} AND EXISTS ${CMAKE_CXX_COMPILER}) ${CMAKE_CURRENT_SOURCE_DIR}/aicpu_sharder/aicpu_pulse.cc ${CMAKE_CURRENT_SOURCE_DIR}/aicpu_sharder/aicpu_sharder.cc ${CMAKE_CURRENT_SOURCE_DIR}/random_choice_with_mask_kernels.cc + ${CMAKE_CURRENT_SOURCE_DIR}/environ/aicpu_environ_manager.cc + ${CMAKE_CURRENT_SOURCE_DIR}/environ/environ_create.cc + ${CMAKE_CURRENT_SOURCE_DIR}/environ/environ_set.cc + ${CMAKE_CURRENT_SOURCE_DIR}/environ/environ_get.cc + ${CMAKE_CURRENT_SOURCE_DIR}/environ/environ_destroy_all.cc ) add_library(mindspore_aicpu_kernels SHARED diff --git a/mindspore/ccsrc/backend/kernel_compiler/aicpu/aicpu_ops/common/kernel_log.h b/mindspore/ccsrc/backend/kernel_compiler/aicpu/aicpu_ops/common/kernel_log.h index df9b593828b..70be4f8fca6 100644 --- a/mindspore/ccsrc/backend/kernel_compiler/aicpu/aicpu_ops/common/kernel_log.h +++ b/mindspore/ccsrc/backend/kernel_compiler/aicpu/aicpu_ops/common/kernel_log.h @@ -1,77 +1,95 @@ -/** - * Copyright 2021 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#ifndef AICPU_OPS_AICPU_COMMON_KERNEL_LOG_H_ -#define AICPU_OPS_AICPU_COMMON_KERNEL_LOG_H_ - -#include -#include -#include -#include -#include "common/kernel_errcode.h" - -inline int GetTid(void) { - thread_local static int tid = syscall(__NR_gettid); - return tid; -} -static const int LOG_COUNT = 0; - -namespace aicpu { -#define AICPU_LOG_DEBUG 0 -#define AICPU_LOG_INFO 1 -#define AICPU_LOG_WARN 2 -#define AICPU_LOG_ERROR 3 -#define AICPU_LOG_EVENT 0x10 - -inline void PrintLog(const int level) { std::cerr << level << std::endl; } - -template -inline void PrintLog(const int level, T &&head, Args &&... tail) { - std::cerr << std::forward(head) << " "; - PrintLog(level, std::forward(tail)...); -} - -int LogSetLevel(int level); - -int LogGetLevel(void); - -bool CheckLogLevel(int log_level_check); - -#define AICPU_LOGD(fmt, ...) \ - AICPU_LOG(AICPU_LOG_DEBUG, "%s:%s:%d[tid:%lu]:" #fmt, __FUNCTION__, __FILE__, __LINE__, GetTid(), ##__VA_ARGS__); -#define AICPU_LOGI(fmt, ...) \ - AICPU_LOG(AICPU_LOG_INFO, "%s:%s:%d[tid:%lu]:" #fmt, __FUNCTION__, __FILE__, __LINE__, GetTid(), ##__VA_ARGS__); -#define AICPU_LOGW(fmt, ...) \ - AICPU_LOG(AICPU_LOG_WARN, "%s:%s:%d[tid:%lu]:" #fmt, __FUNCTION__, __FILE__, __LINE__, GetTid(), ##__VA_ARGS__); -#define AICPU_LOGE(fmt, ...) \ - AICPU_LOG(AICPU_LOG_ERROR, "%s:%s:%d[tid:%lu]:" #fmt, __FUNCTION__, __FILE__, __LINE__, GetTid(), ##__VA_ARGS__); -#define AICPU_LOGEVENT(fmt, ...) \ - AICPU_LOG(AICPU_LOG_EVENT, "%s:%s:%d[tid:%lu]:" #fmt, __FUNCTION__, __FILE__, __LINE__, GetTid(), ##__VA_ARGS__); -#define AICPU_LOG(level, fmt, ...) \ - do { \ - if (aicpu::CheckLogLevel(level)) { \ - aicpu::PrintLog(level, "[%s:%d]" fmt, __FILE__, __LINE__, ##__VA_ARGS__); \ - } \ - } while (LOG_COUNT != 0) - -#define AICPU_CHK_STATUS_RET(expr...) \ - do { \ - const uint32_t status = (expr); \ - if (status != AICPU_KERNEL_STATE_SUCCESS) { \ - return status; \ - } \ - } while (0); -} // namespace aicpu -#endif // AICPU_OPS_AICPU_COMMON_KERNEL_LOG_H_ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef AICPU_OPS_AICPU_COMMON_KERNEL_LOG_H_ +#define AICPU_OPS_AICPU_COMMON_KERNEL_LOG_H_ + +#include +#include +#include +#include +#include "common/kernel_errcode.h" + +inline int GetTid(void) { + thread_local static int tid = syscall(__NR_gettid); + return tid; +} +static const int LOG_COUNT = 0; + +namespace aicpu { +#define AICPU_LOG_DEBUG 0 +#define AICPU_LOG_INFO 1 +#define AICPU_LOG_WARN 2 +#define AICPU_LOG_ERROR 3 +#define AICPU_LOG_EVENT 0x10 + +inline void PrintLog(const int level) { std::cerr << level << std::endl; } + +template +inline void PrintLog(const int level, T &&head, Args &&... tail) { + std::cerr << std::forward(head) << " "; + PrintLog(level, std::forward(tail)...); +} + +int LogSetLevel(int level); + +int LogGetLevel(void); + +bool CheckLogLevel(int log_level_check); + +#define AICPU_LOGD(fmt, ...) \ + AICPU_LOG(AICPU_LOG_DEBUG, "%s:%s:%d[tid:%lu]:" #fmt, __FUNCTION__, __FILE__, __LINE__, GetTid(), ##__VA_ARGS__); +#define AICPU_LOGI(fmt, ...) \ + AICPU_LOG(AICPU_LOG_INFO, "%s:%s:%d[tid:%lu]:" #fmt, __FUNCTION__, __FILE__, __LINE__, GetTid(), ##__VA_ARGS__); +#define AICPU_LOGW(fmt, ...) \ + AICPU_LOG(AICPU_LOG_WARN, "%s:%s:%d[tid:%lu]:" #fmt, __FUNCTION__, __FILE__, __LINE__, GetTid(), ##__VA_ARGS__); +#define AICPU_LOGE(fmt, ...) \ + AICPU_LOG(AICPU_LOG_ERROR, "%s:%s:%d[tid:%lu]:" #fmt, __FUNCTION__, __FILE__, __LINE__, GetTid(), ##__VA_ARGS__); +#define AICPU_LOGEVENT(fmt, ...) \ + AICPU_LOG(AICPU_LOG_EVENT, "%s:%s:%d[tid:%lu]:" #fmt, __FUNCTION__, __FILE__, __LINE__, GetTid(), ##__VA_ARGS__); +#define AICPU_LOG(level, fmt, ...) \ + do { \ + if (aicpu::CheckLogLevel(level)) { \ + aicpu::PrintLog(level, "[%s:%d]" fmt, __FILE__, __LINE__, ##__VA_ARGS__); \ + } \ + } while (LOG_COUNT != 0) + +#define AICPU_CHK_STATUS_RET(expr...) \ + do { \ + const uint32_t status = (expr); \ + if (status != AICPU_KERNEL_STATE_SUCCESS) { \ + return status; \ + } \ + } while (0); + +#define AICPU_CHECK_NULLPTR_VOID(value, logText...) \ + if (value == nullptr) { \ + AICPU_LOGE(logText); \ + return; \ + } + +#define AICPU_CHECK_FALSE(condition, errorCode, logText...) \ + if (!(condition)) { \ + AICPU_LOGE(logText); \ + return errorCode; \ + } + +#define AICPU_CHECK_NULLPTR(value, errorCode, logText...) \ + if (value == nullptr) { \ + AICPU_LOGE(logText); \ + return errorCode; \ + } +} // namespace aicpu +#endif // AICPU_OPS_AICPU_COMMON_KERNEL_LOG_H_ diff --git a/mindspore/ccsrc/backend/kernel_compiler/aicpu/aicpu_ops/environ/aicpu_environ.h b/mindspore/ccsrc/backend/kernel_compiler/aicpu/aicpu_ops/environ/aicpu_environ.h new file mode 100644 index 00000000000..72d5ca2efd0 --- /dev/null +++ b/mindspore/ccsrc/backend/kernel_compiler/aicpu/aicpu_ops/environ/aicpu_environ.h @@ -0,0 +1,97 @@ +/** + * Copyright 2022 Huawei Technologies Co., Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_AICPU_AICPU_OPS_ENVIRON_AICPU_ENVIRON_H_ +#define MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_AICPU_AICPU_OPS_ENVIRON_AICPU_ENVIRON_H_ + +#include +#include +#include +#include +#include "common/kernel_log.h" + +namespace aicpu { +constexpr int64_t kObjectTypeTensorType = 17; +constexpr int64_t kObjectTypeEnvType = 26; +// index of input or output +enum Index : size_t { + kIndex0 = 0, + kIndex1, + kIndex2, + kIndex3, + kIndex4, + kIndex5, + kIndex6, + kIndex7, + kIndex8, + kIndex9, + kIndex10, + kIndex11, + kIndex12, + kIndex13, + kIndex14, + kIndex15, + kIndex16, +}; + +struct EnvironValue { + EnvironValue() : addr_(nullptr), size_(0), value_type_(kObjectTypeTensorType) {} + EnvironValue(void *address_addr, size_t address_size, int32_t value_type) + : addr_(address_addr), size_(address_size), value_type_(value_type) {} + + void *addr_; + size_t size_; + int32_t value_type_; +}; + +using EnvironValuePtr = std::shared_ptr; + +// Environ is the meaning expression of map. +class Environ { + public: + explicit Environ(int64_t handle) : handle_(handle) {} + virtual ~Environ() = default; + + void Set(int64_t key, const EnvironValuePtr &value) { values_[key] = value; } + + EnvironValuePtr Get(int64_t key) { + if (values_.count(key) > 0) { + return values_[key]; + } + return nullptr; + } + + void Clear() { + // Foreach values to free the value addr. + for (auto &value : values_) { + AICPU_CHECK_NULLPTR_VOID(value.second, "value.second is null."); + free(value.second->addr_); + } + values_.clear(); + handle_ = 0; + } + + private: + // The handle is unique for each env. + int64_t handle_ = 0; + + // Store the tensors in map, as . + std::map values_; +}; +using EnvironPtr = std::shared_ptr; +} // namespace aicpu + +#endif // MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_AICPU_AICPU_OPS_ENVIRON_AICPU_ENVIRON_H_ diff --git a/mindspore/ccsrc/backend/kernel_compiler/aicpu/aicpu_ops/environ/aicpu_environ_manager.cc b/mindspore/ccsrc/backend/kernel_compiler/aicpu/aicpu_ops/environ/aicpu_environ_manager.cc new file mode 100644 index 00000000000..708fcd7be1a --- /dev/null +++ b/mindspore/ccsrc/backend/kernel_compiler/aicpu/aicpu_ops/environ/aicpu_environ_manager.cc @@ -0,0 +1,100 @@ +/** + * Copyright 2022 Huawei Technologies Co., Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "environ/aicpu_environ_manager.h" +#include + +namespace aicpu { +constexpr auto kScalarTensorShapeDim = 1; +constexpr auto kScalarTensorShapeSize = 1; +constexpr auto kEnvValueTypeAttr = "value_type"; + +int64_t EnvironMgr::Create() { + std::unique_lock lock(mutex); + if (env_handles_count_ >= INT64_MAX) { + AICPU_LOGE(" The handles number:%d is out of range: ", env_handles_count_); + return AICPU_KERNEL_STATE_PARAM_INVALID; + } + int64_t ret_handle = ++env_handles_count_; + auto env = std::make_shared(ret_handle); + AICPU_CHECK_NULLPTR(env, AICPU_KERNEL_STATE_PARAM_INVALID, "env is null."); + envs_[ret_handle] = env; + + return ret_handle; +} + +EnvironPtr EnvironMgr::Get(int64_t handle) { + std::unique_lock lock(mutex); + const auto &envIter = envs_.find(handle); + if (envIter != envs_.end()) { + auto &result = envIter->second; + return result; + } + return nullptr; +} + +void EnvironMgr::Clear() { + std::unique_lock lock(mutex); + for (auto &env : envs_) { + AICPU_CHECK_NULLPTR_VOID(env.second, "env is null.") + env.second->Clear(); + } + envs_.clear(); +} + +bool EnvironMgr::IsScalarTensor(const aicpuops::Tensor &tensor) { + aicpuops::TensorShape shape = tensor.tensor_shape(); + if (shape.dim_size() == 0) { + AICPU_LOGD("The shape is empty."); + return true; + } + + if ((shape.dim_size() == kScalarTensorShapeDim) && (shape.dim(aicpu::kIndex0).size() == kScalarTensorShapeSize)) { + AICPU_LOGD("The tensor is scalar."); + return true; + } + return false; +} + +bool EnvironMgr::CheckEnvInput(const aicpuops::NodeDef &node_def) { + ::google::protobuf::Map<::std::string, ::aicpuops::AttrValue> nodedef_map = node_def.attrs(); + auto value_type_attr = nodedef_map[kEnvValueTypeAttr].i(); + if ((value_type_attr != kObjectTypeTensorType) && (value_type_attr != kObjectTypeEnvType)) { + AICPU_LOGE("The value type is not supported: [%d]", value_type_attr); + return false; + } + + // Check the input handle. + if (!IsScalarTensor(node_def.inputs(aicpu::kIndex0))) { + AICPU_LOGE("The input handle checks invalid."); + return false; + } + + // Check the input key + if (!IsScalarTensor(node_def.inputs(aicpu::kIndex1))) { + AICPU_LOGE("The input key checks invalid."); + return false; + } + + // Check the input value + if ((value_type_attr == kObjectTypeEnvType) && (!IsScalarTensor(node_def.inputs(aicpu::kIndex2)))) { + AICPU_LOGE("The input value checks invalid."); + return false; + } + + return true; +} +} // namespace aicpu diff --git a/mindspore/ccsrc/backend/kernel_compiler/aicpu/aicpu_ops/environ/aicpu_environ_manager.h b/mindspore/ccsrc/backend/kernel_compiler/aicpu/aicpu_ops/environ/aicpu_environ_manager.h new file mode 100644 index 00000000000..966164ed8e1 --- /dev/null +++ b/mindspore/ccsrc/backend/kernel_compiler/aicpu/aicpu_ops/environ/aicpu_environ_manager.h @@ -0,0 +1,69 @@ +/** + * Copyright 2022 Huawei Technologies Co., Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_AICPU_AICPU_OPS_ENVIRON_AICPU_ENVIRON_MANAGER_H_ +#define MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_AICPU_AICPU_OPS_ENVIRON_AICPU_ENVIRON_MANAGER_H_ + +#include +#include +#include +#include +#include +#include "environ/aicpu_environ.h" +#include "aicpu_sharder/aicpu_sharder.h" +#include "proto/aicpu_tensor.pb.h" +#include "common/distinct_uniform_int_distribution.h" +#include "common/tensor.h" + +namespace aicpu { +class EnvironMgr { + public: + static EnvironMgr &GetInstance() noexcept { + static EnvironMgr instance; + return instance; + } + + EnvironMgr(const EnvironMgr &) = delete; + EnvironMgr(EnvironMgr &&) = delete; + EnvironMgr &operator=(const EnvironMgr &) = delete; + EnvironMgr &operator=(EnvironMgr &&) = delete; + + // Create the env object and return the unique env handle. + int64_t Create(); + + EnvironPtr Get(int64_t handle); + + void Clear(); + + // Check whether the inputs of EnvironGet kernel or EnvironSet kernel are valid. + bool CheckEnvInput(const aicpuops::NodeDef &node_def); + // Check whether is scalar tensor. Environ handle and env key only support scalar tensor currently. + bool IsScalarTensor(const aicpuops::Tensor &tensor); + + private: + EnvironMgr() = default; + ~EnvironMgr() = default; + + // Store the envs in map, as . + std::map envs_; + + int64_t env_handles_count_{0}; + + std::mutex mutex; +}; +} // namespace aicpu + +#endif // MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_AICPU_AICPU_OPS_ENVIRON_AICPU_ENVIRON_MANAGER_H_ diff --git a/mindspore/ccsrc/backend/kernel_compiler/aicpu/aicpu_ops/environ/environ_create.cc b/mindspore/ccsrc/backend/kernel_compiler/aicpu/aicpu_ops/environ/environ_create.cc new file mode 100644 index 00000000000..32f51c08e65 --- /dev/null +++ b/mindspore/ccsrc/backend/kernel_compiler/aicpu/aicpu_ops/environ/environ_create.cc @@ -0,0 +1,46 @@ +/** + * Copyright 2022 Huawei Technologies Co., Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "environ/environ_create.h" +#include "environ/aicpu_environ_manager.h" + +namespace aicpu { +uint32_t EnvironCreateKernel::DoCompute() { + // Generate an unique handle. + int64_t env_handle = EnvironMgr::GetInstance().Create(); + AICPU_LOGD("Create env handle:%d", env_handle); + auto *output_data = reinterpret_cast(io_addrs_[aicpu::kIndex0]); + output_data[0] = env_handle; + + return AICPU_KERNEL_STATE_SUCCESS; +} + +uint32_t EnvironCreateKernel::ParseKernelParam() { + AICPU_LOGD("Enter ParseKernelParam."); + if (!EnvironMgr::GetInstance().IsScalarTensor(node_def_.outputs(aicpu::kIndex0))) { + AICPU_LOGE("The output is not scalar tensor."); + return AICPU_KERNEL_STATE_PARAM_INVALID; + } + return AICPU_KERNEL_STATE_SUCCESS; +} +} // namespace aicpu + +extern "C" { +__attribute__((visibility("default"))) uint32_t EnvironCreate(void *param) { + aicpu::EnvironCreateKernel environCreateKernel; + return environCreateKernel.Compute(param); +} +} diff --git a/mindspore/ccsrc/backend/kernel_compiler/aicpu/aicpu_ops/environ/environ_create.h b/mindspore/ccsrc/backend/kernel_compiler/aicpu/aicpu_ops/environ/environ_create.h new file mode 100644 index 00000000000..6e036c2a940 --- /dev/null +++ b/mindspore/ccsrc/backend/kernel_compiler/aicpu/aicpu_ops/environ/environ_create.h @@ -0,0 +1,33 @@ +/** + * Copyright 2022 Huawei Technologies Co., Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_AICPU_AICPU_OPS_ENVIRON_ENVIRON_CREATE_H_ +#define MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_AICPU_AICPU_OPS_ENVIRON_ENVIRON_CREATE_H_ + +#include "common/kernel_base.h" + +namespace aicpu { +class EnvironCreateKernel : public KernelBase { + public: + EnvironCreateKernel() : KernelBase("EnvironCreate") {} + ~EnvironCreateKernel() = default; + + protected: + uint32_t DoCompute() override; + uint32_t ParseKernelParam() override; +}; +} // namespace aicpu +#endif // MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_AICPU_AICPU_OPS_ENVIRON_ENVIRON_CREATE_H_ diff --git a/mindspore/ccsrc/backend/kernel_compiler/aicpu/aicpu_ops/environ/environ_destroy_all.cc b/mindspore/ccsrc/backend/kernel_compiler/aicpu/aicpu_ops/environ/environ_destroy_all.cc new file mode 100644 index 00000000000..0df1dc8eb1b --- /dev/null +++ b/mindspore/ccsrc/backend/kernel_compiler/aicpu/aicpu_ops/environ/environ_destroy_all.cc @@ -0,0 +1,42 @@ +/** + * Copyright 2022 Huawei Technologies Co., Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "environ/environ_destroy_all.h" +#include "environ/aicpu_environ_manager.h" + +namespace aicpu { +uint32_t EnvironDestroyAllKernel::DoCompute() { + AICPU_LOGD("Destroy all env handle"); + EnvironMgr::GetInstance().Clear(); + return AICPU_KERNEL_STATE_SUCCESS; +} + +uint32_t EnvironDestroyAllKernel::ParseKernelParam() { + AICPU_LOGD("Enter ParseKernelParam."); + if (!EnvironMgr::GetInstance().IsScalarTensor(node_def_.outputs(aicpu::kIndex0))) { + AICPU_LOGE("The output is not scalar tensor."); + return AICPU_KERNEL_STATE_PARAM_INVALID; + } + return AICPU_KERNEL_STATE_SUCCESS; +} +} // namespace aicpu + +extern "C" { +__attribute__((visibility("default"))) uint32_t EnvironDestroyAll(void *param) { + aicpu::EnvironDestroyAllKernel environDestroyAllKernel; + return environDestroyAllKernel.Compute(param); +} +} diff --git a/mindspore/ccsrc/backend/kernel_compiler/aicpu/aicpu_ops/environ/environ_destroy_all.h b/mindspore/ccsrc/backend/kernel_compiler/aicpu/aicpu_ops/environ/environ_destroy_all.h new file mode 100644 index 00000000000..ee791b10fe3 --- /dev/null +++ b/mindspore/ccsrc/backend/kernel_compiler/aicpu/aicpu_ops/environ/environ_destroy_all.h @@ -0,0 +1,33 @@ +/** + * Copyright 2022 Huawei Technologies Co., Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_AICPU_AICPU_OPS_ENVIRON_ENVIRON_DESTORY_ALL_H_ +#define MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_AICPU_AICPU_OPS_ENVIRON_ENVIRON_DESTORY_ALL_H_ + +#include "common/kernel_base.h" + +namespace aicpu { +class EnvironDestroyAllKernel : public KernelBase { + public: + EnvironDestroyAllKernel() : KernelBase("EnvironDestroyAll") {} + ~EnvironDestroyAllKernel() = default; + + protected: + uint32_t DoCompute() override; + uint32_t ParseKernelParam() override; +}; +} // namespace aicpu +#endif // MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_AICPU_AICPU_OPS_ENVIRON_ENVIRON_DESTORY_ALL_H_ diff --git a/mindspore/ccsrc/backend/kernel_compiler/aicpu/aicpu_ops/environ/environ_get.cc b/mindspore/ccsrc/backend/kernel_compiler/aicpu/aicpu_ops/environ/environ_get.cc new file mode 100644 index 00000000000..748d621d8a3 --- /dev/null +++ b/mindspore/ccsrc/backend/kernel_compiler/aicpu/aicpu_ops/environ/environ_get.cc @@ -0,0 +1,107 @@ +/** + * Copyright 2022 Huawei Technologies Co., Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "environ/environ_get.h" +#include +#include +#include +#include +#include +#include "mindspore/ccsrc/backend/kernel_compiler/aicpu/aicpu_ops/aicpu_sharder/aicpu_sharder.h" +#include "mindspore/ccsrc/backend/kernel_compiler/aicpu/aicpu_ops/common/tensor.h" +#include "environ/aicpu_environ_manager.h" + +namespace aicpu { +constexpr auto kEnvValueTypeAttr = "value_type"; + +uint32_t EnvironGetKernel::DoCompute() { + AICPU_LOGD("Enter DoCompute."); + auto &env_mgr = EnvironMgr::GetInstance(); + + auto *input_handle_ptr = reinterpret_cast((io_addrs_[aicpu::kIndex0])); + auto *input_key_ptr = reinterpret_cast((io_addrs_[aicpu::kIndex1])); + auto *default_value_ptr = reinterpret_cast((io_addrs_[aicpu::kIndex2])); + auto *output_ptr = reinterpret_cast((io_addrs_[aicpu::kIndex3])); + + // Get handle and key + int64_t handle = input_handle_ptr[0]; + int64_t key = input_key_ptr[0]; + + // Get env and value by handle and key + const auto &env = env_mgr.Get(handle); + AICPU_CHECK_NULLPTR(env, AICPU_KERNEL_STATE_PARAM_INVALID, "Get env [%d] failed", handle) + const auto &env_value = env->Get(key); + + AICPU_LOGD("EnvironGetKernel: hindle[%d], key[%d], value[%d]", handle, key, (void *)&env_value); + // Default value + auto *output_value_ptr = default_value_ptr; + auto output_value_size = default_value_size_; + auto output_value_type = attr_value_type_; + if (env_value != nullptr) { + output_value_ptr = env_value->addr_; + output_value_size = env_value->size_; + output_value_type = env_value->value_type_; + } else { + AICPU_LOGE("Get key[%d] value checks failed.", key); + } + + if ((output_value_size_ < output_value_size) || (output_value_type != attr_value_type_)) { + AICPU_LOGE("The env value checks invalid, value_size: %d vs %d, value_type:%d vs %d", output_value_size_, + output_value_size, output_value_type, attr_value_type_); + return AICPU_KERNEL_STATE_PARAM_INVALID; + } + + auto ret = memcpy_s(output_ptr, output_value_size_, output_value_ptr, output_value_size_); + AICPU_CHECK_FALSE((ret == EOK), AICPU_KERNEL_STATE_PARAM_INVALID, + "Memcpy size[%zu] from env map to output[0] failed.", output_value_size_); + + return AICPU_KERNEL_STATE_SUCCESS; +} + +uint32_t EnvironGetKernel::ParseKernelParam() { + AICPU_LOGD("Enter ParseKernelParam."); + auto &env_mgr = EnvironMgr::GetInstance(); + if (!env_mgr.CheckEnvInput(node_def_)) { + AICPU_LOGE("The input checks invalid. "); + return AICPU_KERNEL_STATE_PARAM_INVALID; + } + + // Get value type attr + ::google::protobuf::Map<::std::string, ::aicpuops::AttrValue> nodedef_map = node_def_.attrs(); + attr_value_type_ = nodedef_map[kEnvValueTypeAttr].i(); + + // check output value + auto default_value_tensor = node_def_.inputs(aicpu::kIndex2); + auto output_value_ptr_tensor = node_def_.outputs(aicpu::kIndex0); + if ((output_value_ptr_tensor.tensor_shape().dim_size() != default_value_tensor.tensor_shape().dim_size()) || + (output_value_ptr_tensor.tensor_type() != default_value_tensor.tensor_type())) { + AICPU_LOGE("The env value checks invalid."); + return AICPU_KERNEL_STATE_PARAM_INVALID; + } + + // Get value size. + default_value_size_ = default_value_tensor.data_size(); + output_value_size_ = output_value_ptr_tensor.data_size(); + return AICPU_KERNEL_STATE_SUCCESS; +} +} // namespace aicpu + +extern "C" { +__attribute__((visibility("default"))) uint32_t EnvironGet(void *param) { + aicpu::EnvironGetKernel environGetKernel; + return environGetKernel.Compute(param); +} +} diff --git a/mindspore/ccsrc/backend/kernel_compiler/aicpu/aicpu_ops/environ/environ_get.h b/mindspore/ccsrc/backend/kernel_compiler/aicpu/aicpu_ops/environ/environ_get.h new file mode 100644 index 00000000000..1d490658831 --- /dev/null +++ b/mindspore/ccsrc/backend/kernel_compiler/aicpu/aicpu_ops/environ/environ_get.h @@ -0,0 +1,39 @@ +/** + * Copyright 2022 Huawei Technologies Co., Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_AICPU_AICPU_OPS_ENVIRON_ENVIRON_GET_H_ +#define MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_AICPU_AICPU_OPS_ENVIRON_ENVIRON_GET_H_ + +#include +#include "mindspore/ccsrc/backend/kernel_compiler/aicpu/aicpu_ops/common/kernel_base.h" + +namespace aicpu { +class EnvironGetKernel : public KernelBase { + public: + EnvironGetKernel() : KernelBase("EnvironGet") {} + ~EnvironGetKernel() = default; + + protected: + uint32_t DoCompute() override; + uint32_t ParseKernelParam() override; + + private: + int32_t attr_value_type_{0}; + size_t default_value_size_{0}; + size_t output_value_size_{0}; +}; +} // namespace aicpu +#endif // MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_AICPU_AICPU_OPS_ENVIRON_ENVIRON_GET_H_ diff --git a/mindspore/ccsrc/backend/kernel_compiler/aicpu/aicpu_ops/environ/environ_set.cc b/mindspore/ccsrc/backend/kernel_compiler/aicpu/aicpu_ops/environ/environ_set.cc new file mode 100644 index 00000000000..e26bdcf343a --- /dev/null +++ b/mindspore/ccsrc/backend/kernel_compiler/aicpu/aicpu_ops/environ/environ_set.cc @@ -0,0 +1,85 @@ +/** + * Copyright 2022 Huawei Technologies Co., Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "environ/environ_set.h" +#include +#include +#include "mindspore/ccsrc/backend/kernel_compiler/aicpu/aicpu_ops/aicpu_sharder/aicpu_sharder.h" +#include "mindspore/ccsrc/backend/kernel_compiler/aicpu/aicpu_ops/common/tensor.h" +#include "environ/aicpu_environ_manager.h" + +namespace aicpu { +constexpr auto kEnvValueTypeAttr = "value_type"; + +uint32_t EnvironSetKernel::DoCompute() { + AICPU_LOGD("Enter DoCompute."); + auto &env_mgr = EnvironMgr::GetInstance(); + + auto *input_handle_ptr = reinterpret_cast(io_addrs_[aicpu::kIndex0]); + auto *input_key_ptr = reinterpret_cast(io_addrs_[aicpu::kIndex1]); + auto *input_value_ptr = reinterpret_cast(io_addrs_[aicpu::kIndex2]); + auto *output_handle_ptr = reinterpret_cast(io_addrs_[aicpu::kIndex3]); + + auto *value_ptr = malloc(value_size_); + AICPU_CHECK_NULLPTR(value_ptr, AICPU_KERNEL_STATE_PARAM_INVALID, "Malloc failed.") + auto ret = memcpy_s(value_ptr, value_size_, input_value_ptr, value_size_); + AICPU_CHECK_FALSE((ret == EOK), AICPU_KERNEL_STATE_PARAM_INVALID, "Memcpy size from input[2] to environ failed.", + value_size_); + + // Set env member. + const auto &env = env_mgr.Get(input_handle_ptr[0]); + AICPU_CHECK_NULLPTR(env, AICPU_KERNEL_STATE_PARAM_INVALID, "Get handle[%d] failed.", input_handle_ptr[0]); + + auto env_value = std::make_shared(value_ptr, value_size_, attr_value_type_); + env->Set(input_key_ptr[0], env_value); + AICPU_LOGD("EnvironSetKernel: handle[%d], key[%d], value[%d]", input_handle_ptr[0], input_key_ptr[0], + (void *)&env_value); + + // Set output handle + output_handle_ptr[0] = input_handle_ptr[0]; + return AICPU_KERNEL_STATE_SUCCESS; +} + +uint32_t EnvironSetKernel::ParseKernelParam() { + AICPU_LOGD("Enter ParseKernelParam."); + auto &env_mgr = EnvironMgr::GetInstance(); + if (!env_mgr.CheckEnvInput(node_def_)) { + AICPU_LOGE("The input checks invalid. "); + return AICPU_KERNEL_STATE_PARAM_INVALID; + } + + if (!env_mgr.IsScalarTensor(node_def_.outputs(aicpu::kIndex0))) { + AICPU_LOGE("The output handle is not equal of input handle."); + return AICPU_KERNEL_STATE_PARAM_INVALID; + } + + // Get value type. + ::google::protobuf::Map<::std::string, ::aicpuops::AttrValue> nodedef_map = node_def_.attrs(); + attr_value_type_ = nodedef_map[kEnvValueTypeAttr].i(); + + // Get value size. + aicpuops::Tensor value_tensor = node_def_.inputs(aicpu::kIndex2); + value_size_ = value_tensor.data_size(); + return AICPU_KERNEL_STATE_SUCCESS; +} +} // namespace aicpu + +extern "C" { +__attribute__((visibility("default"))) uint32_t EnvironSet(void *param) { + aicpu::EnvironSetKernel environSetKernel; + return environSetKernel.Compute(param); +} +} diff --git a/mindspore/ccsrc/backend/kernel_compiler/aicpu/aicpu_ops/environ/environ_set.h b/mindspore/ccsrc/backend/kernel_compiler/aicpu/aicpu_ops/environ/environ_set.h new file mode 100644 index 00000000000..14e25a632ce --- /dev/null +++ b/mindspore/ccsrc/backend/kernel_compiler/aicpu/aicpu_ops/environ/environ_set.h @@ -0,0 +1,36 @@ +/** + * Copyright 2022 Huawei Technologies Co., Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_AICPU_AICPU_OPS_ENVIRON_ENVIRON_SET_H_ +#define MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_AICPU_AICPU_OPS_ENVIRON_ENVIRON_SET_H_ + +#include "common/kernel_base.h" +namespace aicpu { +class EnvironSetKernel : public KernelBase { + public: + EnvironSetKernel() : KernelBase("EnvironSet") {} + ~EnvironSetKernel() = default; + + protected: + uint32_t DoCompute() override; + uint32_t ParseKernelParam() override; + + private: + int32_t attr_value_type_{0}; + size_t value_size_{0}; +}; +} // namespace aicpu +#endif // MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_AICPU_AICPU_OPS_ENVIRON_ENVIRON_SET_H_ diff --git a/mindspore/ccsrc/backend/kernel_compiler/aicpu/aicpu_util.h b/mindspore/ccsrc/backend/kernel_compiler/aicpu/aicpu_util.h index 851a71d69f4..ae63b3bdf94 100644 --- a/mindspore/ccsrc/backend/kernel_compiler/aicpu/aicpu_util.h +++ b/mindspore/ccsrc/backend/kernel_compiler/aicpu/aicpu_util.h @@ -71,11 +71,16 @@ constexpr auto kSearchSorted = "SearchSorted"; constexpr auto kResizeBilinear = "ResizeBilinear"; constexpr auto kResizeBilinearGrad = "ResizeBilinearGrad"; constexpr auto kScatterElements = "ScatterElements"; +constexpr auto kEnvironCreate = "EnvironCreate"; +constexpr auto kEnvironSet = "EnvironSet"; +constexpr auto kEnvironGet = "EnvironGet"; +constexpr auto kEnvironDestroyAll = "EnvironDestroyAll"; const std::set kCpuKernelOps{kIdentity, kMaskedSelect, kMaskedSelectGrad, kDynamicStitch, kSearchSorted, kResizeBilinear, kResizeBilinearGrad, kScatterElements}; const std::set kCacheKernelOps{kUpdateCache, kCacheSwapTable, kSubAndFilter, kPadAndShift, kDropout3D, kDropout2D, kNonMaxSuppressionV3}; -const std::set kCpuKernelBaseOps{kGetNext, kInitData, kRandomChoiceWithMask}; +const std::set kCpuKernelBaseOps{kGetNext, kInitData, kRandomChoiceWithMask, kEnvironCreate, + kEnvironSet, kEnvironGet, kEnvironDestroyAll}; const std::set kDynamicInputOps{ kPrint, kPack, kMeshgrid, kStackInitOpName, kStackDestroyOpName, kStackPushOpName, kStackPopOpName, kDynamicStitch}; struct AicpuParamHead { diff --git a/mindspore/ccsrc/backend/optimizer/ascend/ascend_backend_optimization.cc b/mindspore/ccsrc/backend/optimizer/ascend/ascend_backend_optimization.cc index d26360b9311..b529a595d73 100644 --- a/mindspore/ccsrc/backend/optimizer/ascend/ascend_backend_optimization.cc +++ b/mindspore/ccsrc/backend/optimizer/ascend/ascend_backend_optimization.cc @@ -139,6 +139,7 @@ #include "backend/optimizer/ascend/enhancer/add_attr_for_3d_graph.h" #include "backend/optimizer/ascend/enhancer/split_n_optimizer.h" #include "backend/optimizer/ascend/mindir/space_batch_nd_attr_update.h" +#include "backend/optimizer/ascend/mindir/env_op_attr_update.h" #include "backend/optimizer/ascend/mindir/dropout_unify_mindir.h" #include "backend/optimizer/ascend/mindir/maxpool_to_maxpool_with_argmax.h" #include "backend/optimizer/ascend/mindir/maxpool_with_argmax_unify_mindir.h" @@ -575,6 +576,7 @@ void AscendUnifyMindIR(const std::shared_ptr &graph) { auto unify_mindir_pm = std::make_shared("unify_mindir_pm"); unify_mindir_pm->AddPass(std::make_shared()); unify_mindir_pm->AddPass(std::make_shared()); + unify_mindir_pm->AddPass(std::make_shared()); unify_mindir_pm->AddPass(std::make_shared()); unify_mindir_pm->AddPass(std::make_shared()); unify_mindir_pm->AddPass(std::make_shared()); diff --git a/mindspore/ccsrc/backend/optimizer/ascend/mindir/env_op_attr_update.cc b/mindspore/ccsrc/backend/optimizer/ascend/mindir/env_op_attr_update.cc new file mode 100644 index 00000000000..fa690aa2744 --- /dev/null +++ b/mindspore/ccsrc/backend/optimizer/ascend/mindir/env_op_attr_update.cc @@ -0,0 +1,46 @@ +/** + * Copyright 2022 Huawei Technologies Co., Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "backend/optimizer/ascend/mindir/env_op_attr_update.h" +#include +#include +#include "utils/utils.h" +#include "backend/session/anf_runtime_algorithm.h" + +namespace mindspore { +namespace opt { +const AnfNodePtr EnvOpAttrUpdate::Process(const FuncGraphPtr &graph, const AnfNodePtr &node, + const EquivPtr &equiv) const { + MS_EXCEPTION_IF_NULL(graph); + MS_EXCEPTION_IF_NULL(node); + MS_EXCEPTION_IF_NULL(equiv); + + static const std::set kEnvOpNames = {kEnvironCreateOpName, kEnvironSetOpName, kEnvironGetOpName, + kEnvironDestroyAllOpName}; + static const std::string kEnvOpSoNames = "mindspore_aicpu_kernels"; + + if (!node->isa()) { + return node; + } + auto kernel_name = AnfAlgo::GetCNodeName(node); + if (kEnvOpNames.find(kernel_name) != kEnvOpNames.end()) { + AnfAlgo::SetNodeAttr(kAttrCustAicpu, MakeValue(kEnvOpSoNames), node); + } + + return node; +} +} // namespace opt +} // namespace mindspore diff --git a/mindspore/ccsrc/backend/optimizer/ascend/mindir/env_op_attr_update.h b/mindspore/ccsrc/backend/optimizer/ascend/mindir/env_op_attr_update.h new file mode 100644 index 00000000000..325ccdcca60 --- /dev/null +++ b/mindspore/ccsrc/backend/optimizer/ascend/mindir/env_op_attr_update.h @@ -0,0 +1,31 @@ +/** + * Copyright 2022 Huawei Technologies Co., Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef MINDSPORE_CCSRC_BACKEND_OPTIMIZER_ASCEND_MINDIR_ENV_OP_ATTR_UPDATE_H_ +#define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_ASCEND_MINDIR_ENV_OP_ATTR_UPDATE_H_ + +#include "backend/optimizer/common/optimizer.h" + +namespace mindspore { +namespace opt { +class EnvOpAttrUpdate : public PatternProcessPass { + public: + explicit EnvOpAttrUpdate(bool multigraph = true) : PatternProcessPass("env_op_attr_update", multigraph) {} + ~EnvOpAttrUpdate() override = default; + const AnfNodePtr Process(const FuncGraphPtr &, const AnfNodePtr &, const EquivPtr &) const override; +}; +} // namespace opt +} // namespace mindspore +#endif // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_ASCEND_MINDIR_ENV_OP_ATTR_UPDATE_H_ diff --git a/mindspore/ccsrc/utils/utils.h b/mindspore/ccsrc/utils/utils.h index 17886f98a26..00ad50b7065 100644 --- a/mindspore/ccsrc/utils/utils.h +++ b/mindspore/ccsrc/utils/utils.h @@ -337,6 +337,10 @@ constexpr auto kNPUClearFloatStatusOpName = "NPUClearFloatStatus"; constexpr auto kAssignOpName = "Assign"; constexpr auto kScatterAddOpName = "ScatterAdd"; constexpr auto kScatterUpdateOpName = "ScatterUpdate"; +constexpr auto kEnvironCreateOpName = "EnvironCreate"; +constexpr auto kEnvironSetOpName = "EnvironSet"; +constexpr auto kEnvironGetOpName = "EnvironGet"; +constexpr auto kEnvironDestroyAllOpName = "EnvironDestroyAll"; // Communication world group constexpr auto kNcclWorldGroup = "nccl_world_group"; diff --git a/mindspore/python/mindspore/ops/_op_impl/aicpu/__init__.py b/mindspore/python/mindspore/ops/_op_impl/aicpu/__init__.py index 4c407c1462f..a2c5e7bffc1 100644 --- a/mindspore/python/mindspore/ops/_op_impl/aicpu/__init__.py +++ b/mindspore/python/mindspore/ops/_op_impl/aicpu/__init__.py @@ -103,6 +103,10 @@ from .lower_bound import _lower_bound_aicpu from .upper_bound import _upper_bound_aicpu from .grid_sampler_3d import _grid_sampler_3d_aicpu from .grid_sampler_3d_grad import _grid_sampler_3d_grad_aicpu +from .environ_create import _environ_create_aicpu +from .environ_set import _environ_set_aicpu +from .environ_get import _environ_get_aicpu +from .environ_destroy_all import _environ_destroy_all_aicpu from .cross import _cross_aicpu from .cummax import _cummax_aicpu from .floor_div import _floor_div_aicpu diff --git a/mindspore/python/mindspore/ops/_op_impl/aicpu/environ_create.py b/mindspore/python/mindspore/ops/_op_impl/aicpu/environ_create.py new file mode 100644 index 00000000000..eb6c0573c2a --- /dev/null +++ b/mindspore/python/mindspore/ops/_op_impl/aicpu/environ_create.py @@ -0,0 +1,28 @@ +# Copyright 2022 Huawei Technologies Co., Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================ + +"""EnvironCreate op""" +from mindspore.ops.op_info_register import op_info_register, AiCPURegOp, DataType + +environ_create_op_info = AiCPURegOp("EnvironCreate") \ + .fusion_type("OPAQUE") \ + .output(0, "handle", "required") \ + .dtype_format(DataType.I64_Default) \ + .get_op_info() + +@op_info_register(environ_create_op_info) +def _environ_create_aicpu(): + """EnvironCreate AiCPU register""" + return diff --git a/mindspore/python/mindspore/ops/_op_impl/aicpu/environ_destroy_all.py b/mindspore/python/mindspore/ops/_op_impl/aicpu/environ_destroy_all.py new file mode 100644 index 00000000000..f231e1aa4fc --- /dev/null +++ b/mindspore/python/mindspore/ops/_op_impl/aicpu/environ_destroy_all.py @@ -0,0 +1,28 @@ +# Copyright 2022 Huawei Technologies Co., Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================ + +"""EnvironDestroyAll op""" +from mindspore.ops.op_info_register import op_info_register, AiCPURegOp, DataType + +environ_destroy_all_op_info = AiCPURegOp("EnvironDestroyAll") \ + .fusion_type("OPAQUE") \ + .output(0, "result", "required") \ + .dtype_format(DataType.BOOL_Default) \ + .get_op_info() + +@op_info_register(environ_destroy_all_op_info) +def _environ_destroy_all_aicpu(): + """EnvironDestroyAll AiCPU register""" + return diff --git a/mindspore/python/mindspore/ops/_op_impl/aicpu/environ_get.py b/mindspore/python/mindspore/ops/_op_impl/aicpu/environ_get.py new file mode 100644 index 00000000000..46b8d4b03a9 --- /dev/null +++ b/mindspore/python/mindspore/ops/_op_impl/aicpu/environ_get.py @@ -0,0 +1,41 @@ +# Copyright 2022 Huawei Technologies Co., Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================ + +"""EnvironGet op""" +from mindspore.ops.op_info_register import op_info_register, AiCPURegOp, DataType + +environ_get_op_info = AiCPURegOp("EnvironGet") \ + .fusion_type("OPAQUE") \ + .attr("value_type", "int") \ + .input(0, "env", "required") \ + .input(1, "key", "required") \ + .input(2, "default", "required") \ + .output(0, "value", "required") \ + .dtype_format(DataType.I64_Default, DataType.I64_Default, DataType.I64_Default, DataType.I64_Default) \ + .dtype_format(DataType.I64_Default, DataType.I64_Default, DataType.I32_Default, DataType.I32_Default) \ + .dtype_format(DataType.I64_Default, DataType.I64_Default, DataType.I16_Default, DataType.I16_Default) \ + .dtype_format(DataType.I64_Default, DataType.I64_Default, DataType.U32_Default, DataType.U32_Default) \ + .dtype_format(DataType.I64_Default, DataType.I64_Default, DataType.U16_Default, DataType.U16_Default) \ + .dtype_format(DataType.I64_Default, DataType.I64_Default, DataType.U8_Default, DataType.U8_Default) \ + .dtype_format(DataType.I64_Default, DataType.I64_Default, DataType.U64_Default, DataType.U64_Default) \ + .dtype_format(DataType.I64_Default, DataType.I64_Default, DataType.F32_Default, DataType.F32_Default) \ + .dtype_format(DataType.I64_Default, DataType.I64_Default, DataType.F16_Default, DataType.F16_Default) \ + .dtype_format(DataType.I64_Default, DataType.I64_Default, DataType.BOOL_Default, DataType.BOOL_Default) \ + .get_op_info() + +@op_info_register(environ_get_op_info) +def _environ_get_aicpu(): + """EnvironGet AiCPU register""" + return diff --git a/mindspore/python/mindspore/ops/_op_impl/aicpu/environ_set.py b/mindspore/python/mindspore/ops/_op_impl/aicpu/environ_set.py new file mode 100644 index 00000000000..5a176b41d8b --- /dev/null +++ b/mindspore/python/mindspore/ops/_op_impl/aicpu/environ_set.py @@ -0,0 +1,40 @@ +# Copyright 2022 Huawei Technologies Co., Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================ + +"""EnvironSet op""" +from mindspore.ops.op_info_register import op_info_register, AiCPURegOp, DataType + +environ_set_op_info = AiCPURegOp("EnvironSet") \ + .fusion_type("OPAQUE") \ + .attr("value_type", "int") \ + .input(0, "env", "required") \ + .input(1, "key", "required") \ + .input(2, "value", "required") \ + .output(0, "env", "required") \ + .dtype_format(DataType.I64_Default, DataType.I64_Default, DataType.I64_Default, DataType.I64_Default) \ + .dtype_format(DataType.I64_Default, DataType.I64_Default, DataType.I32_Default, DataType.I64_Default) \ + .dtype_format(DataType.I64_Default, DataType.I64_Default, DataType.I16_Default, DataType.I64_Default) \ + .dtype_format(DataType.I64_Default, DataType.I64_Default, DataType.U32_Default, DataType.I64_Default) \ + .dtype_format(DataType.I64_Default, DataType.I64_Default, DataType.U8_Default, DataType.I64_Default) \ + .dtype_format(DataType.I64_Default, DataType.I64_Default, DataType.U64_Default, DataType.I64_Default) \ + .dtype_format(DataType.I64_Default, DataType.I64_Default, DataType.F16_Default, DataType.I64_Default) \ + .dtype_format(DataType.I64_Default, DataType.I64_Default, DataType.F32_Default, DataType.I64_Default) \ + .dtype_format(DataType.I64_Default, DataType.I64_Default, DataType.BOOL_Default, DataType.I64_Default) \ + .get_op_info() + +@op_info_register(environ_set_op_info) +def _environ_set_aicpu(): + """EnvironSet AiCPU register""" + return diff --git a/tests/st/control/inner/test_120_if_after_while_in_if.py b/tests/st/control/inner/test_120_if_after_while_in_if.py index e324e8defe9..b012672ac20 100644 --- a/tests/st/control/inner/test_120_if_after_while_in_if.py +++ b/tests/st/control/inner/test_120_if_after_while_in_if.py @@ -77,7 +77,6 @@ def test_forward(): @pytest.mark.level0 @pytest.mark.platform_x86_gpu_training -@pytest.mark.platform_x86_cpu @pytest.mark.platform_arm_ascend_training @pytest.mark.platform_x86_ascend_training @pytest.mark.env_onecard diff --git a/tests/st/ops/ascend/test_aicpu_ops/test_env_ops.py b/tests/st/ops/ascend/test_aicpu_ops/test_env_ops.py new file mode 100644 index 00000000000..7ca9323ac61 --- /dev/null +++ b/tests/st/ops/ascend/test_aicpu_ops/test_env_ops.py @@ -0,0 +1,59 @@ +# Copyright 2022 Huawei Technologies Co., Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================ +""" test grad ops """ +import mindspore.ops as ops +from mindspore import ms_function +from mindspore import Tensor, context +from mindspore.common import dtype as mstype + +one = Tensor([1], mstype.int32) +zero = Tensor([0], mstype.int32) + +@ms_function +def local_pow(x, n): + r = one + while n > zero: + n = n - one + r = r * x + return r + +def test_pow_first_order(): + """ + Feature: pow first order test. + Description: pow first order test. + Expectation: compile done without error. + """ + context.set_context(mode=context.GRAPH_MODE) + x = Tensor([5], mstype.int32) + n = Tensor([3], mstype.int32) + grad = ops.GradOperation() + grad_net = grad(local_pow) + res = grad_net(x, n) + assert res == 75 + +def test_pow_second_order(): + """ + Feature: pow second order test. + Description: pow second order test. + Expectation: compile done without error. + """ + context.set_context(mode=context.GRAPH_MODE) + x = Tensor([5], mstype.int32) + n = Tensor([3], mstype.int32) + grad = ops.GradOperation() + grad_net = grad(local_pow) + sec_grad_net = grad(grad_net) + res = sec_grad_net(x, n) + assert res == 30