def init_by_initializer_conf( var_blob: EagerBlobTrait, initializer_conf: initializer_conf_util.InitializerConf, sync_between_multi_machine: bool, random_seed=0, ): initializer = initializer_util.GetInitializer(initializer_conf, random_seed, var_blob.shape) # initializer is None if and only if the initializer_conf is empty_initializer if initializer is None: return def GenerateValueAndAssign(var_blob, start_nd_idx, stop_nd_idx): np_dtype = np.dtype( dtype_util.convert_oneflow_dtype_to_numpy_dtype(var_blob.dtype)) length = _ElemCnt(np.array(stop_nd_idx) - np.array(start_nd_idx)) vals = (np.array(initializer(length)).astype(np_dtype).reshape( np.array(stop_nd_idx) - np.array(start_nd_idx))) slice_value_blob = _GetCpu0VariableBlobFromNumpy(vals, var_blob.dtype) _LogicalSliceAssign( var_blob, slice_value_blob, start_nd_idx, stop_nd_idx, ) # we just want to run f on every slice without caring about the return value for _ in _ForEachSlice(var_blob, GenerateValueAndAssign): pass if sync_between_multi_machine: oneflow_api.eager.Sync()
def _init_eager_local_tensor_by_initializer_conf(eager_local_tensor, initializer_conf, random_seed=0): shape = tuple(eager_local_tensor.shape) initializer = initializer_util.GetInitializer(initializer_conf, random_seed, shape) # initializer is None if and only if the initializer_conf is empty_initializer if initializer is None: return _copy_from_numpy_to_eager_local_tensor( eager_local_tensor, check_point_v2.generate_values_by_initializer( initializer, shape, eager_local_tensor.dtype), )
def Init() -> None: oneflow.sync_default_session() sess = session_ctx.GetDefaultSession() for op_name, var_blob in GetAllVariables().items(): var_conf = sess.OpAttribute4InterfaceOpName( op_name).op_conf.variable_conf if not (var_conf.HasField("initializer") or var_conf.HasField("initialize_with_snapshot")): continue if var_conf.HasField("initialize_with_snapshot"): initialize_with_snapshot_conf = var_conf.initialize_with_snapshot if initialize_with_snapshot_conf.HasField("key"): snapshot_key = op_name else: snapshot_key = initialize_with_snapshot_conf.key var_dir = os.path.dirname( os.path.join( initialize_with_snapshot_conf.path, snapshot_key, )) LoadVariables({op_name: GetCheckpoint(var_dir)}) continue g = initializer_util.GetInitializer(var_conf.initializer, var_conf.random_seed, var_blob.shape) def GenerateValueAndAssign(var_blob, start_nd_idx, stop_nd_idx): np_dtype = np.dtype( dtype_util.convert_oneflow_dtype_to_numpy_dtype( var_blob.dtype)) length = _ElemCnt(np.array(stop_nd_idx) - np.array(start_nd_idx)) vals = (np.array(g(length)).astype(np_dtype).reshape( np.array(stop_nd_idx) - np.array(start_nd_idx))) slice_value_blob = _GetCpu0VariableBlobFromNumpy( vals, var_blob.dtype) _LogicalSliceAssign( var_blob, slice_value_blob, start_nd_idx, stop_nd_idx, ) # we just want to run f on every slice without caring about the return value for _ in _ForEachSlice(var_blob, GenerateValueAndAssign): pass oneflow_api.eager.Sync()
def init_by_initializer_conf( var_blob: Union[EagerBlobTrait, "oneflow.Tensor"], initializer_conf: initializer_conf_util.InitializerConf, sync_between_multi_machine: bool, scope_symbol_id: Optional[int], random_seed: int = 0, ): initializer = initializer_util.GetInitializer(initializer_conf, random_seed, var_blob.shape) # initializer is None if and only if the initializer_conf is empty_initializer if initializer is None: return def GenerateValueAndAssign(var_blob, start_nd_idx, stop_nd_idx): np_dtype = np.dtype( dtype_util.convert_oneflow_dtype_to_numpy_dtype(var_blob.dtype)) length = _ElemCnt(np.array(stop_nd_idx) - np.array(start_nd_idx)) vals = (np.array(initializer(length)).astype(np_dtype).reshape( np.array(stop_nd_idx) - np.array(start_nd_idx))) if isinstance(var_blob, oneflow.Tensor): var_blob_object = var_blob._blob_object else: assert isinstance(var_blob, EagerBlobTrait) var_blob_object = var_blob.blob_object slice_value_blob = _GetCpu0VariableBlobFromNumpy(vals, var_blob.dtype) _LogicalSliceAssign( var_blob_object, slice_value_blob.blob_object, start_nd_idx, stop_nd_idx, scope_symbol_id, ) # we just want to run f on every slice without caring about the return value for _ in _ForEachSlice(var_blob, GenerateValueAndAssign): pass if sync_between_multi_machine: oneflow_api.eager.single_client.Sync()
def init_by_initializer_conf( var_blob: Union[EagerBlobTrait, "oneflow.Tensor"], initializer_conf: initializer_conf_util.InitializerConf, sync_between_multi_machine: bool, scope_symbol_id: Optional[int], random_seed: int = 0, ): initializer = initializer_util.GetInitializer(initializer_conf, random_seed, var_blob.shape) # initializer is None if and only if the initializer_conf is empty_initializer if initializer is None: return def GenerateValueAndAssign(var_blob, start_nd_idx, stop_nd_idx): shape = np.array(stop_nd_idx) - np.array(start_nd_idx) vals = generate_values_by_initializer(initializer, shape, var_blob.dtype) if isinstance(var_blob, oneflow.Tensor): raise ValueError("Tensor object arguments are not supported") else: assert isinstance(var_blob, EagerBlobTrait) var_blob_object = var_blob.blob_object slice_value_blob = _GetCpu0VariableBlobFromNumpy(vals, var_blob.dtype) _LogicalSliceAssign( var_blob_object, slice_value_blob.blob_object, start_nd_idx, stop_nd_idx, scope_symbol_id, ) # we just want to run f on every slice without caring about the return value for _ in _ForEachSlice(var_blob, GenerateValueAndAssign): pass if sync_between_multi_machine: oneflow._oneflow_internal.eager.single_client.Sync()