diff --git a/mindnlp/core/_prims/gpu/__init__.py b/examples/llama_factory/.gitkeep similarity index 100% rename from mindnlp/core/_prims/gpu/__init__.py rename to examples/llama_factory/.gitkeep diff --git a/examples/transformers/mindpilot/README.md b/examples/mindpilot/README.md similarity index 100% rename from examples/transformers/mindpilot/README.md rename to examples/mindpilot/README.md diff --git a/examples/transformers/classification/bert_emotect_finetune.ipynb b/examples/tasks/classification/bert_emotect_finetune.ipynb similarity index 100% rename from examples/transformers/classification/bert_emotect_finetune.ipynb rename to examples/tasks/classification/bert_emotect_finetune.ipynb diff --git a/examples/transformers/classification/bert_imdb_finetune.ipynb b/examples/tasks/classification/bert_imdb_finetune.ipynb similarity index 100% rename from examples/transformers/classification/bert_imdb_finetune.ipynb rename to examples/tasks/classification/bert_imdb_finetune.ipynb diff --git a/examples/transformers/classification/bilstm_imdb_concise.ipynb b/examples/tasks/classification/bilstm_imdb_concise.ipynb similarity index 100% rename from examples/transformers/classification/bilstm_imdb_concise.ipynb rename to examples/tasks/classification/bilstm_imdb_concise.ipynb diff --git a/examples/transformers/classification/bilstm_imdb_scratch.ipynb b/examples/tasks/classification/bilstm_imdb_scratch.ipynb similarity index 100% rename from examples/transformers/classification/bilstm_imdb_scratch.ipynb rename to examples/tasks/classification/bilstm_imdb_scratch.ipynb diff --git a/examples/transformers/classification/dual_bert_imdb_scratch.ipynb b/examples/tasks/classification/dual_bert_imdb_scratch.ipynb similarity index 100% rename from examples/transformers/classification/dual_bert_imdb_scratch.ipynb rename to examples/tasks/classification/dual_bert_imdb_scratch.ipynb diff --git a/examples/transformers/classification/gpt_imdb_finetune.ipynb b/examples/tasks/classification/gpt_imdb_finetune.ipynb similarity index 100% rename from examples/transformers/classification/gpt_imdb_finetune.ipynb rename to examples/tasks/classification/gpt_imdb_finetune.ipynb diff --git a/examples/transformers/classification/nezha_classification.ipynb b/examples/tasks/classification/nezha_classification.ipynb similarity index 100% rename from examples/transformers/classification/nezha_classification.ipynb rename to examples/tasks/classification/nezha_classification.ipynb diff --git a/examples/transformers/language_model/fasttext.py b/examples/tasks/language_model/fasttext.py similarity index 100% rename from examples/transformers/language_model/fasttext.py rename to examples/tasks/language_model/fasttext.py diff --git a/examples/transformers/machine_translation/seq2seq_scratch.ipynb b/examples/tasks/machine_translation/seq2seq_scratch.ipynb similarity index 100% rename from examples/transformers/machine_translation/seq2seq_scratch.ipynb rename to examples/tasks/machine_translation/seq2seq_scratch.ipynb diff --git a/examples/transformers/question_answer/bidaf/__init__.py b/examples/tasks/question_answer/bidaf/__init__.py similarity index 100% rename from examples/transformers/question_answer/bidaf/__init__.py rename to examples/tasks/question_answer/bidaf/__init__.py diff --git a/examples/transformers/question_answer/bidaf/evaluate.py b/examples/tasks/question_answer/bidaf/evaluate.py similarity index 100% rename from examples/transformers/question_answer/bidaf/evaluate.py rename to examples/tasks/question_answer/bidaf/evaluate.py diff --git a/examples/transformers/question_answer/bidaf/squad_process.py b/examples/tasks/question_answer/bidaf/squad_process.py similarity index 100% rename from examples/transformers/question_answer/bidaf/squad_process.py rename to examples/tasks/question_answer/bidaf/squad_process.py diff --git a/examples/transformers/question_answer/bidaf_squad_concise.ipynb b/examples/tasks/question_answer/bidaf_squad_concise.ipynb similarity index 100% rename from examples/transformers/question_answer/bidaf_squad_concise.ipynb rename to examples/tasks/question_answer/bidaf_squad_concise.ipynb diff --git a/examples/transformers/question_answer/bidaf_squad_scratch.ipynb b/examples/tasks/question_answer/bidaf_squad_scratch.ipynb similarity index 100% rename from examples/transformers/question_answer/bidaf_squad_scratch.ipynb rename to examples/tasks/question_answer/bidaf_squad_scratch.ipynb diff --git a/examples/transformers/sequence_labeling/Bert-LSTM-CRF.ipynb b/examples/tasks/sequence_labeling/Bert-LSTM-CRF.ipynb similarity index 100% rename from examples/transformers/sequence_labeling/Bert-LSTM-CRF.ipynb rename to examples/tasks/sequence_labeling/Bert-LSTM-CRF.ipynb diff --git a/examples/transformers/sequence_labeling/LSTM-CRF.ipynb b/examples/tasks/sequence_labeling/LSTM-CRF.ipynb similarity index 100% rename from examples/transformers/sequence_labeling/LSTM-CRF.ipynb rename to examples/tasks/sequence_labeling/LSTM-CRF.ipynb diff --git a/examples/transformers/sequence_labeling/conll2003/test.txt b/examples/tasks/sequence_labeling/conll2003/test.txt similarity index 100% rename from examples/transformers/sequence_labeling/conll2003/test.txt rename to examples/tasks/sequence_labeling/conll2003/test.txt diff --git a/examples/transformers/sequence_labeling/conll2003/train.txt b/examples/tasks/sequence_labeling/conll2003/train.txt similarity index 100% rename from examples/transformers/sequence_labeling/conll2003/train.txt rename to examples/tasks/sequence_labeling/conll2003/train.txt diff --git a/examples/transformers/sequence_labeling/conll2003/valid.txt b/examples/tasks/sequence_labeling/conll2003/valid.txt similarity index 100% rename from examples/transformers/sequence_labeling/conll2003/valid.txt rename to examples/tasks/sequence_labeling/conll2003/valid.txt diff --git a/examples/transformers/tensor_decomposition/dual_bert_td.ipynb b/examples/tasks/tensor_decomposition/dual_bert_td.ipynb similarity index 100% rename from examples/transformers/tensor_decomposition/dual_bert_td.ipynb rename to examples/tasks/tensor_decomposition/dual_bert_td.ipynb diff --git a/examples/transformers/text_generation/gpt2_summarization.ipynb b/examples/tasks/text_generation/gpt2_summarization.ipynb similarity index 100% rename from examples/transformers/text_generation/gpt2_summarization.ipynb rename to examples/tasks/text_generation/gpt2_summarization.ipynb diff --git a/examples/transformers/text_generation/gpt2_summarization.py b/examples/tasks/text_generation/gpt2_summarization.py similarity index 100% rename from examples/transformers/text_generation/gpt2_summarization.py rename to examples/tasks/text_generation/gpt2_summarization.py diff --git a/mindnlp/core/_dtype.py b/mindnlp/core/_dtype.py index d480e7f9c..c291d87e6 100644 --- a/mindnlp/core/_dtype.py +++ b/mindnlp/core/_dtype.py @@ -6,6 +6,8 @@ from .configs import ON_A1 +bool_alias = bool + if ON_A1: warnings.warn('MindSpore on GPU/910A do not support bfloat16, use float16 instead.') bfloat16 = float16 @@ -98,4 +100,8 @@ def __gt__(self, other): float16 : np.float16, float32 : np.float32, float64 : np.float64, -} \ No newline at end of file +} + +py2dtype = { + bool_alias: bool +} diff --git a/mindnlp/core/_prims/ascend.py b/mindnlp/core/_prims/ascend.py index e9911115d..626bed220 100644 --- a/mindnlp/core/_prims/ascend.py +++ b/mindnlp/core/_prims/ascend.py @@ -137,6 +137,12 @@ def argmax_with_value(*args): __all__.append('argmax_with_value') +def argmin_with_value(*args): + return pyboost_inner_prim.argmin_with_value_impl(*args) + +__all__.append('argmin_with_value') + + right_shift_op = ops.RightShift().set_device('Ascend') def right_shift(input, other): if isinstance(other, numbers.Number): diff --git a/mindnlp/core/_prims/gpu.py b/mindnlp/core/_prims/gpu.py new file mode 100644 index 000000000..e69de29bb diff --git a/mindnlp/core/_prims/meta.py b/mindnlp/core/_prims/meta.py index 1f97a8b5f..08bf032a9 100644 --- a/mindnlp/core/_prims/meta.py +++ b/mindnlp/core/_prims/meta.py @@ -262,3 +262,32 @@ def concat(tensors, dim): return core.Tensor(out) __all__.append('concat') + +def tril_ext(input, k): + return input + +__all__.append('tril_ext') + +def reshape(input, shape): + out = Tensor_(shape=tuple(shape), dtype=input.dtype) + return core.Tensor(out) + +__all__.append('reshape') + +def linalg_vector_norm(input, p, dim, keepdim, dtype): + input_shape = list(input.shape) + if isinstance(dim, int): + dim = (dim,) + for d in dim: + input_shape[d] = 1 if keepdim else 0 + + new_shape = [] + for s in input_shape: + if s != 0: + new_shape.append(s) + if dtype is None: + dtype = input.dtype + out = Tensor_(shape=tuple(new_shape), dtype=dtype) + return core.Tensor(out) + +__all__.append('linalg_vector_norm') \ No newline at end of file diff --git a/mindnlp/core/_prims/numpy.py b/mindnlp/core/_prims/numpy.py index e6e40326a..52bcda153 100644 --- a/mindnlp/core/_prims/numpy.py +++ b/mindnlp/core/_prims/numpy.py @@ -61,6 +61,8 @@ def sub_ext(input, other, alpha): elif not isinstance(other, numbers.Number): other = other.numpy() out = np.subtract(input, other * alpha) + if not isinstance(out, np.ndarray): + out = np.array(out) return core.Tensor.from_numpy(out) __all__.append('sub_ext') @@ -503,3 +505,9 @@ def less_equal(input, other): return core.Tensor.from_numpy(out) __all__.append('less_equal') + +def tril_ext(input, diagonal): + out = np.tril(input.numpy(), diagonal) + return core.Tensor.from_numpy(out) + +__all__.append('tril_ext') diff --git a/mindnlp/core/ops/creation.py b/mindnlp/core/ops/creation.py index 6db903bc4..f382e4857 100644 --- a/mindnlp/core/ops/creation.py +++ b/mindnlp/core/ops/creation.py @@ -23,7 +23,7 @@ def from_numpy(ndarray): # frombuffer def frombuffer(buffer, *, dtype, count=-1, offset=0, requires_grad=False): - arr = np.frombuffer(buffer=buffer, dtype=core.dtype_to_nptype(dtype), count=count, offset=offset) + arr = np.frombuffer(buffer=buffer, dtype=core.dtype2np[dtype], count=count, offset=offset) tensor = core.Tensor(arr) tensor.requires_grad_(requires_grad) return tensor @@ -62,6 +62,8 @@ def zeros_like(input, *, dtype=None, layout=None, device=None, requires_grad=Fal def ones(*size, out=None, dtype=None, layout=None, device=None, requires_grad=False): if dtype is None: dtype = get_default_dtype() + if isinstance(dtype, type): + dtype = core.py2dtype[dtype] if device is None: device = get_device_in_context() if isinstance(size[0], (tuple, list)): diff --git a/mindnlp/core/ops/other.py b/mindnlp/core/ops/other.py index f9f0f6fbb..ec1920926 100644 --- a/mindnlp/core/ops/other.py +++ b/mindnlp/core/ops/other.py @@ -866,7 +866,7 @@ def iinfo(dtype): return iinfo_dtype[dtype] def iinfo(dtype): - return np.iinfo(mindspore.dtype_to_nptype(dtype)) + return np.iinfo(core.dtype2np[dtype]) def contains(self, key): diff --git a/mindnlp/core/ops/reduction.py b/mindnlp/core/ops/reduction.py index b5be90bb4..dec11cbf9 100644 --- a/mindnlp/core/ops/reduction.py +++ b/mindnlp/core/ops/reduction.py @@ -59,7 +59,7 @@ def min(input, dim=None, keepdim=False, *, out=None): return execute('min', input) if core.is_tensor(dim): return core.minimum(input, dim) - output = execute('argmin_ext', input, dim, keepdim) + output = execute('argmin_with_value', input, dim, keepdim) if out is None: return min_out(values=output[1], indices=output[0]) diff --git a/mindnlp/factory/__init__.py b/mindnlp/factory/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/mindnlp/factory/cli.py b/mindnlp/factory/cli.py new file mode 100644 index 000000000..e69de29bb