def rebuild_shm_parameter(shm, shape, dtype, requires_grad): def delete_shm(): shm.close() shm.unlink() arr = np.ndarray(shape, dtype=dtype, buffer=shm.buf) t = flow.from_numpy(arr) t._register_storage_delete_hook(delete_shm) return Parameter(t, requires_grad=requires_grad)
def test_same_data(test_case): np_arr = np.random.randn(3, 4, 5) tensor = flow.from_numpy(np_arr) test_case.assertTrue(np.array_equal(np_arr, tensor.numpy())) test_case.assertEqual(tensor.size(), (3, 4, 5)) test_case.assertEqual(tensor.stride(), (20, 5, 1)) test_case.assertEqual(tensor.storage_offset(), 0) np_arr[1:2, 2:3, 3:4] = random.random() test_case.assertTrue(np.array_equal(np_arr, tensor.numpy()))
def as_tensor(data, dtype=None, device=None): if flow.is_tensor(data): if dtype is None: dtype = data.dtype if device is None: device = data.device if data.dtype is dtype and data.device is device: return data else: data = data.to(dtype=dtype, device=device) elif isinstance(data, (np.ndarray)): if dtype is None: if (device is None) or (device.type == "cpu"): data = flow.from_numpy(data) else: data = flow.tensor(data, device=device) else: if data.dtype in numpy_dtype_to_oneflow_dtype_dict: data_infer_flow_type = numpy_dtype_to_oneflow_dtype_dict[ data.dtype] else: raise TypeError( "numpy-ndarray holds elements of unsupported datatype") if data_infer_flow_type is dtype: if (device is None) or (device.type == "cpu"): data = flow.from_numpy(data) else: data = flow.tensor(data, dtype=dtype, device=device) else: if (device is None) or (device.type == "cpu"): data = flow.tensor(data, dtype=dtype) else: data = flow.tensor(data, dtype=dtype, device=device) else: # handle tuple, list, scalar data = np.array(data) # not shared memory in this case data = flow.tensor(data) if device is not None: data = data.to(device) if dtype is not None: data = data.to(dtype) return data
def _test_rnn_utils_pack_padded_sequence(test_case, device): input_size = random.randint(10, 200) max_seq_len = random.randint(10, 500) batch_size = random.randint(10, 500) padded_inputs = np.zeros((max_seq_len, batch_size, input_size)) lengths = [] lengths.append(max_seq_len) for i in range(batch_size - 1): lengths.append(random.randint(1, max_seq_len)) lengths.sort(reverse=True) for i in range(batch_size): padded_inputs[0:lengths[i], i:i + 1, :] = i + 1 inputs = flow.from_numpy(padded_inputs).to(device) flow_res = flow_rnn_utils.pack_padded_sequence(inputs, lengths) torch_inputs = torch.from_numpy(padded_inputs).to(device) torch_res = torch_rnn_utils.pack_padded_sequence(torch_inputs, lengths) test_case.assertTrue( np.allclose( torch_res.batch_sizes.cpu().detach().numpy(), flow_res.batch_sizes.cpu().detach().numpy(), atol=1e-8, )) test_case.assertTrue( np.allclose( torch_res.data.cpu().detach().numpy(), flow_res.data.cpu().detach().numpy(), atol=1e-8, )) torch_seq_unpacked, torch_lens_unpacked = torch_rnn_utils.pad_packed_sequence( torch_res, batch_first=False) flow_seq_unpacked, flow_lens_unpacked = flow_rnn_utils.pad_packed_sequence( flow_res, batch_first=False) test_case.assertTrue( np.allclose( torch_seq_unpacked.cpu().detach().numpy(), flow_seq_unpacked.cpu().detach().numpy(), atol=1e-8, )) test_case.assertTrue( np.allclose( torch_lens_unpacked.cpu().detach().numpy(), flow_lens_unpacked.cpu().detach().numpy(), atol=1e-8, ))
def as_tensor(data, dtype=None, device=None): if flow.is_tensor(data): if dtype is None: dtype = data.dtype if device is None: device = data.device if data.dtype is dtype and data.device is device: return data else: data = data.to(dtype=dtype, device=device) elif isinstance(data, (np.ndarray)): if dtype is None: if (device is None) or (device.type == "cpu"): data = flow.from_numpy(data) else: data = flow.tensor(data, device=device) else: data_infer_flow_type = flow.framework.dtype.convert_numpy_dtype_to_oneflow_dtype( data.dtype ) if data_infer_flow_type is dtype: if (device is None) or (device.type == "cpu"): data = flow.from_numpy(data) else: data = flow.tensor(data, dtype=dtype, device=device) else: if (device is None) or (device.type == "cpu"): data = flow.tensor(data, dtype=dtype) else: data = flow.tensor(data, dtype=dtype, device=device) else: # not shared memory in this case data = flow.tensor(data) if device is not None: data = data.to(device) if dtype is not None: data = data.to(dtype) return data
def rebuild_shm_tensor(shm, shape, dtype, requires_grad): def delete_shm(): shm.close() try: shm.unlink() except: pass arr = np.ndarray(shape, dtype=dtype, buffer=shm.buf) t = flow.from_numpy(arr) t._register_storage_delete_hook(delete_shm) t.requires_grad = requires_grad return t
def test_more_dtype(test_case): for dtype in [ np.float64, np.float32, np.float16, np.int64, np.int32, np.int8, np.uint8, ]: np_arr = np.ones((2, 3), dtype=dtype) tensor = flow.from_numpy(np_arr) # TODO(wyg): oneflow.float16 do not support to copy from tensor to numpy if tensor.dtype not in [flow.float16]: test_case.assertTrue(np.array_equal(np_arr, tensor.numpy()))
def test_use_ops(test_case): np_arr = np.random.randn(3, 4, 5) tensor = flow.from_numpy(np_arr) res = tensor**2 test_case.assertTrue(np.allclose(np_arr**2, res.numpy()))
class_url = "".join([ "https://raw.githubusercontent.com/Cadene/", "pretrained-models.pytorch/master/data/", "imagenet_classes.txt", ]) class_name = "imagenet_classes.txt" class_path = download_testdata(class_url, class_name, module="data") with open(class_path) as f: class_id_to_key = f.readlines() class_id_to_key = [x.strip() for x in class_id_to_key] # Get top-1 result for TVM top1_tvm = np.argmax(tvm_output.numpy()[0]) tvm_class_key = class_id_to_key[top1_tvm] # Convert input to OneFlow variable and get OneFlow result for comparison with flow.no_grad(): torch_img = flow.from_numpy(img) output = model(torch_img) # Get top-1 result for OneFlow top_oneflow = np.argmax(output.numpy()) oneflow_class_key = class_id_to_key[top_oneflow] print("Relay top-1 id: {}, class name: {}".format( top1_tvm, key_to_classname[tvm_class_key])) print("OneFlow top-1 id: {}, class name: {}".format( top_oneflow, key_to_classname[oneflow_class_key]))
def test_non_contiguous_input(test_case): np_arr = np.random.randn(4, 5) np_arr = np_arr.transpose(1, 0) tensor = flow.from_numpy(np_arr) # TODO(wyg): support non-contiguous input test_case.assertTrue(tensor.is_contiguous())
def np_to_global(np): t = flow.from_numpy(np) return t.to_global(placement=flow.env.all_device_placement("cpu"), sbp=flow.sbp.broadcast)