def test_1d_ring_buffer_Wm_assign_Wc_plus_1(test_case): if flow.eager_execution_enabled(): return device_name = "0:0" flow.config.cpu_device_num(2) buffer_size = 4 @flow.global_function() def Foo() -> tp.Numpy: with flow.scope.placement("cpu", device_name): w = flow.get_variable( "w", shape=(10,), dtype=flow.float, initializer=flow.constant_initializer(0), ) ones = flow.constant_like(w, value=1.0, dtype=flow.float) (ref, value) = flow.experimental.ssp_variable_proxy( w, buffer_size=buffer_size ) flow.assign(ref, value + ones) return value zeros = np.zeros((10,)).astype(np.float32) ones = np.ones((10,)).astype(np.float32) for i in range(buffer_size): x = Foo() test_case.assertTrue(np.allclose(x, zeros)) for i in range(buffer_size): x = Foo() test_case.assertTrue(np.allclose(x, ones)) for i in range(buffer_size): x = Foo() test_case.assertTrue(np.allclose(x, ones + ones))
def decode_ofrecord( ofrecord_dir: str, blobs: Sequence[BlobConf], batch_size: int = 1, data_part_num: int = 1, part_name_prefix: str = "part-", part_name_suffix_length: int = -1, shuffle: bool = False, buffer_size: int = 1024, name: str = None, ) -> Tuple[oneflow._oneflow_internal.BlobDesc]: print( "WARNING:", "oneflow.compatible.single_client.data.decode_ofrecord is deprecated, and NOT work in eager mode, please use: \n", " 1) ofrecord = oneflow.compatible.single_client.data.ofrecord_reader(...) to read ofrecord; \n", " 2) image = oneflow.compatible.single_client.data.ofrecord_image_decoder(...) to decode image; \n", " 3) raw = oneflow.compatible.single_client.data.ofrecord_raw_decoder(...) to decode raw data like label; \n", traceback.format_stack()[-2], ) assert not flow.eager_execution_enabled() ofrecord = flow.data.ofrecord_reader( ofrecord_dir=ofrecord_dir, batch_size=batch_size, data_part_num=data_part_num, part_name_prefix=part_name_prefix, part_name_suffix_length=part_name_suffix_length, random_shuffle=shuffle, shuffle_buffer_size=buffer_size, name=name, ) result_blob_list = [] for blob_conf in blobs: result_blob_list.append( blob_conf.decode_blob(input_blob=ofrecord, batch_size=batch_size)) return tuple(result_blob_list)
def test(test_case): flow.config.gpu_device_num(2) @flow.global_function() def add() -> tp.Numpy: with flow.scope.placement("gpu", "0:0-1"): x = flow.get_variable( name="x", shape=(2, 3), initializer=flow.random_uniform_initializer(), ) y = flow.get_variable( name="y", shape=(2, 3), initializer=flow.random_uniform_initializer(), ) return flow.math.add_n([x, y]) flow.train.CheckPoint().init() if flow.eager_execution_enabled(): add() x_value = np.random.random((2, 3)).astype(np.float32) y_value = np.random.random((2, 3)).astype(np.float32) flow.experimental.set_interface_blob_value("x", x_value) flow.experimental.set_interface_blob_value("y", y_value) test_case.assertTrue( np.array_equal(x_value, flow.experimental.get_interface_blob_value("x"))) test_case.assertTrue( np.array_equal(y_value, flow.experimental.get_interface_blob_value("y"))) test_case.assertTrue(np.array_equal(add(), x_value + y_value))
def AddInfo4InterfaceOpName(self, interface_op_name, op_attribute): if flow.eager_execution_enabled(): self.interface_op_name2op_attr_[interface_op_name] = op_attribute self.interface_op_name2job_name_[ interface_op_name] = oneflow._oneflow_internal.JobBuildAndInferCtx_GetCurrentJobName( ) else: pass
def test_eager_assign_121(test_case): if not flow.eager_execution_enabled(): return arg_dict = OrderedDict() arg_dict["shape"] = [10, (30, 4), (8, 256, 20)] arg_dict["dtype"] = [flow.float, flow.double] arg_dict["device_type"] = ["cpu"] arg_dict["assign"] = [flow.experimental.eager_assign_121] for arg in GenArgDict(arg_dict): _compare_with_np(test_case, **arg)
def test_unpack_pack(test_case): if flow.eager_execution_enabled(): return @flow.global_function(function_config=func_config) def UnpackPackJob(a: oft.Numpy.Placeholder((3, 4))): return flow.pack(flow.unpack(a, 3), 3) x = np.random.rand(3, 4).astype(np.float32) y = UnpackPackJob(x).get().numpy() test_case.assertTrue(np.array_equal(y, x))
def unpack(input, unpack_num, name=None): assert not flow.eager_execution_enabled() return ( flow.user_op_builder(name if name is not None else id_util.UniqueStr("Unpack_")) .Op("unpack") .Input("in", [input]) .Output("out") .Attr("unpack_num", unpack_num) .Build() .InferAndTryRun() .RemoteBlobList()[0] )
def acc(one, max_acc_num, name=None): assert not flow.eager_execution_enabled() return ( flow.user_op_builder(name if name is not None else id_util.UniqueStr("Acc_")) .Op("acc") .Input("in", [one]) .Output("out") .Attr("max_acc_num", max_acc_num) .Build() .InferAndTryRun() .RemoteBlobList()[0] )
def test_sparse_softmax_cross_entropy_with_logits(test_case): if flow.eager_execution_enabled(): print("\nSkip under erger mode!") return arg_dict = OrderedDict() arg_dict["device_type"] = ["gpu", "cpu"] arg_dict["data_type"] = ["float32", "double"] arg_dict["label_type"] = ["int32", "int64"] arg_dict["num_classes"] = [1000] arg_dict["batch_size"] = [64] for arg in GenArgList(arg_dict): compare_with_tensorflow(*arg)
def test_add_ssp_variable_proxy(test_case): if flow.eager_execution_enabled(): return device_name = "0:0" flow.config.enable_debug_mode(True) flow.config.cpu_device_num(2) buffer_size = 4 function_config = flow.FunctionConfig() function_config.enable_ssp(True) @flow.global_function(type="train", function_config=function_config) def Foo() -> tp.Numpy: with flow.scope.placement( "cpu", device_name ), flow.experimental.scope.config( ssp_num_stages=buffer_size, ssp_stage_id=0 ): w = flow.get_variable( "w", shape=(10,), dtype=flow.float, initializer=flow.constant_initializer(0), ) loss = w + flow.constant_like(w, value=0.0, dtype=flow.float) flow.optimizer.SGD( flow.optimizer.PiecewiseConstantScheduler([], [-10.0]), momentum=0 ).minimize(loss) return loss zeros = np.zeros((10,)).astype(np.float32) ones = np.ones((10,)).astype(np.float32) for i in range(buffer_size): x = Foo() test_case.assertTrue(np.allclose(x, zeros)) x = Foo() test_case.assertTrue(np.allclose(x, ones)) x = Foo() test_case.assertTrue(np.allclose(x, ones + ones)) x = Foo() test_case.assertTrue(np.allclose(x, ones + ones + ones)) x = Foo() test_case.assertTrue(np.allclose(x, ones + ones + ones + ones))
def RemoteBlobList(self): remote_blob_list = [] for k in self.op_conf_.user_conf.output: if k not in self.output_arg_key_list_: raise ValueError( "output_arg_name {} of {} op is not set in python op builder" .format(k, self.op_conf_.name)) for output_arg_name in self.output_arg_key_list_: assert output_arg_name in self.op_conf_.user_conf.output for i in range( len(self.op_conf_.user_conf.output[output_arg_name].s)): lbi = logical_blob_id_util.LogicalBlobId() lbi.op_name = self.op_conf_.name lbi.blob_name = "{}_{}".format(output_arg_name, i) remote_blob_obj = self.MakeRemoteBlob(lbi) remote_blob_list.append(remote_blob_obj) if flow.eager_execution_enabled(): gradient_util.GetDefaultBackwardBlobRegister( ).TrySetObject4BlobName(remote_blob_obj.logical_blob_name, remote_blob_obj.blob_object) return tuple(remote_blob_list)
def test_testsource(test_case): func_config = flow.FunctionConfig() func_config.default_data_type(flow.float) func_config.default_logical_view(flow.scope.consistent_view()) @flow.global_function(function_config=func_config) def TestSourceJob(): with flow.scope.placement("cpu", "0:0"): ret = my_test_source("my_cc_test_source_op", 0) return ret y = TestSourceJob().get().numpy() rand_0_4 = np.array([0.5488136, 0.59284467, 0.7151894, 0.8442659, 0.6027634]) test_case.assertTrue(np.allclose(y, rand_0_4, atol=1e-05, rtol=1e-05)) y = TestSourceJob().get().numpy() if flow.eager_execution_enabled(): rand_5_9 = rand_0_4 else: rand_5_9 = np.array( [0.85794574, 0.54488325, 0.84725183, 0.42365485, 0.62356377] ) test_case.assertTrue(np.allclose(y, rand_5_9, atol=1e-05, rtol=1e-05))
def test_repeat_acc(test_case, device_type, shape, dtype, acc_num): flow.clear_default_session() if flow.eager_execution_enabled(): return @flow.global_function(function_config=func_config) def RepeatAccJob(a: oft.Numpy.Placeholder(shape)): if dtype == "float16": return flow.cast( flow.acc(flow.repeat(flow.cast(a, flow.float16), acc_num), acc_num), flow.float, ) else: return flow.acc(flow.repeat(a, acc_num), acc_num) x = np.random.rand(*shape).astype(np.float32) y = RepeatAccJob(x).get().numpy() z = x * acc_num if dtype == "float16": z = x.astype(np.float16) * acc_num z = z.astype(np.float32) test_case.assertTrue(np.allclose(y, z, rtol=1e-05, atol=1e-05))
def test_fp16(self): if flow.eager_execution_enabled(): print("\nSkip under erger mode!") return compare_fused_with_no_fused(self, 4, 1024, 12, 64, True)
def test_model_io_case_0(test_case): if flow.eager_execution_enabled(): print("\nSkip under erger mode!") return _test_model_io(test_case, (2, 2), flow.float32, 0.01, 10)
def get_checkpoint_ready_model(model_getter, dtype): model = model_getter(dtype) if flow.eager_execution_enabled(): model() return model