// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. #include "orttraining/training_ops/cuda/math/scale.h" #include "orttraining/training_ops/cuda/math/scale_impl.h" using namespace ONNX_NAMESPACE; using namespace onnxruntime::common; namespace onnxruntime { namespace cuda { #define REGISTER_SCALE_KERNEL_TYPED(T) \ ONNX_OPERATOR_TYPED_KERNEL_EX( \ Scale, \ kMSDomain, \ 1, \ T, \ kCudaExecutionProvider, \ (*KernelDefBuilder::Create()) \ .TypeConstraint("T", DataTypeImpl::GetTensorType()) \ .TypeConstraint("ScaleT", {DataTypeImpl::GetTensorType(), \ DataTypeImpl::GetTensorType(), \ DataTypeImpl::GetTensorType(), \ DataTypeImpl::GetTensorType(), \ DataTypeImpl::GetTensorType(), \ DataTypeImpl::GetTensorType()}) \ .InputMemoryType(OrtMemTypeCPUInput, 1), \ Scale); template struct GetScaleValueImpl { void operator()(const Tensor* scale, float& scale_value) const { ORT_ENFORCE(scale->Shape().Size() == 1, "Scale input should have a single value."); scale_value = static_cast(*(scale->template Data())); ORT_ENFORCE(scale_value != 0.0f, "Scale value must not be 0."); } }; template Scale::Scale(const OpKernelInfo& info) : CudaKernel(info) { int64_t scale_down; info.GetAttrOrDefault("scale_down", &scale_down, static_cast(0)); scale_down_ = (scale_down != 0); } template Status Scale::ComputeInternal(OpKernelContext* context) const { typedef typename ToCudaType::MappedType CudaT; float scale_value; auto scale_tensor = context->Input(1); utils::MLTypeCallDispatcher t_disp(scale_tensor->GetElementType()); t_disp.Invoke(scale_tensor, scale_value); if (scale_down_) { scale_value = 1.0f / scale_value; } auto lhs_tensor = context->Input(0); auto output_tensor = context->Output(0, lhs_tensor->Shape()); Impl_Scale( Stream(context), reinterpret_cast(lhs_tensor->template Data()), scale_value, reinterpret_cast(output_tensor->template MutableData()), output_tensor->Shape().Size()); return Status::OK(); } REGISTER_SCALE_KERNEL_TYPED(MLFloat16) REGISTER_SCALE_KERNEL_TYPED(float) REGISTER_SCALE_KERNEL_TYPED(double) REGISTER_SCALE_KERNEL_TYPED(BFloat16) template Status Scale::ComputeInternal(OpKernelContext* context) const; template Status Scale::ComputeInternal(OpKernelContext* context) const; template Status Scale::ComputeInternal(OpKernelContext* context) const; template Status Scale::ComputeInternal(OpKernelContext* context) const; } // namespace cuda } // namespace onnxruntime