import numbers from tensorflow.python.ops import array_ops from tensorflow.python.framework import tensor_shape from tensorflow.python.framework import dtypes from tensorflow.python.framework import ops # from tensorflow.contrib.offline_train.ops import gen_npu_ops from tensorflow.contrib.util import loader from tensorflow.python.eager import context from tensorflow.python.framework import device from tensorflow.python.framework import ops from tensorflow.python.platform import resource_loader from npu_bridge.estimator.npu.npu_common import NPUBasics from npu_bridge.helper import helper gen_npu_ops = helper.get_gen_ops() DEFAULT_GRAPH_SEED = 87654321 _MAXINT32 = 2**31 - 1 def npu_test(): """A placeholder op for values fed into the TPU simultaneously as a tuple. """ return gen_npu_ops.npu_test() def NPUInit(name=None): if context.executing_eagerly(): raise RuntimeError("tf.NPUInit() is not compatible with " "eager execution.")
# # 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. # ============================================================================== from tensorflow.contrib.util import loader from tensorflow.python.framework import load_library from tensorflow.python.platform import resource_loader from npu_bridge.helper import helper gen_npu_image_ops = helper.get_gen_ops(); def decode_and_resize_jpeg(image, size): """Decode and resize JPEG-encoded image. Args: image: The JPEG-encoded image. size: A 1-D int32 Tensor of 2 elements: new_height, new_width. The new size for the images. Returns: Resized image, a 3-D uint8 tensor: [new_height, new_width, channel=3] . """ return gen_npu_image_ops.decode_and_resize_jpeg(image, size) def decode_and_crop_and_resize_jpeg(image, crop_size, size):
from __future__ import division from __future__ import print_function import numbers from tensorflow.contrib.util import loader from tensorflow.python.platform import resource_loader from tensorflow.python.framework import ops from tensorflow.python.ops import array_ops from tensorflow.python.framework import tensor_shape from tensorflow.python.framework import dtypes from tensorflow.python.eager import context from tensorflow.python.framework import device from npu_bridge.estimator.npu.npu_common import NPUBasics from npu_bridge.helper import helper npu_aicore_ops = helper.get_gen_ops(); @ops.RegisterGradient("FastGelu") def _fast_gelu_grad(op, grad): """The gradient for `fast_gelu`. Args: op: The `fast_gelu` `Operation` that we are differentiating, which we can use to find the inputs and outputs of the original op. grad: Gradient with respect to the output of the `fast_gelu` op. Returns: Gradients with respect to the input of `fast_gelu`. """ return [npu_aicore_ops.fast_gelu_grad(grad, op.inputs[0])] # List of one Tensor, since we have one input
# 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. # ============================================================================== ## @file hccl_ops.py # HCCL 算子API from tensorflow.python.framework import load_library from tensorflow.python.framework import ops from tensorflow.python.platform import resource_loader from npu_bridge.helper import helper gen_hccl_ops = helper.get_gen_ops() ## 提供group内的集合通信allreduce功能 # @param tensor tensorflow的tensor类型,allreduce操作的输入; # @param reduction string类型,reduce的操作类型,可以为”max”,”min”,”prod”和”sum”; # @param fusion int类型,算子融合标识。0: 不融合;1: 按照梯度切分设置融合,默认融; 2: 按照相同fusion_id融合。 # @param fusion_id int类型,算子融合索引标识,相同fusion_id的算子将会融合。 # @param group string类型,group名称,可以为用户自定义group或者"hccl_world_group"; # @return 对输入tensor执行完allreduce操作之后的结果tensor def allreduce(tensor, reduction, fusion=1, fusion_id=-1, group="hccl_world_group"): result = gen_hccl_ops.hcom_all_reduce(input=tensor,
# 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. # ============================================================================== """All bert ops.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from tensorflow.contrib.util import loader from tensorflow.python.platform import resource_loader from tensorflow.python.framework import ops from npu_bridge.helper import helper npu_unary_ops = helper.get_gen_ops() @ops.RegisterGradient("Gelu") def _gelu_grad(op, grad): """The gradient for `gelu`. Args: op: The `gelu` `Operation` that we are differentiating, which we can use to find the inputs and outputs of the original op. grad: Gradient with respect to the output of the `gelu` op. Returns: Gradients with respect to the input of `gelu`. """ return [npu_unary_ops.gelu_grad(grad, op.inputs[0], op.outputs[0])