Browse Source

!15442 modify code method metrics

From: @changzherui
Reviewed-by: @kingxian,@zhoufeng54
Signed-off-by: @kingxian
pull/15442/MERGE
mindspore-ci-bot Gitee 5 years ago
parent
commit
1ecc638fad
3 changed files with 44 additions and 37 deletions
  1. +23
    -19
      mindspore/common/tensor.py
  2. +19
    -16
      mindspore/train/serialization.py
  3. +2
    -2
      tests/ut/python/runtest.sh

+ 23
- 19
mindspore/common/tensor.py View File

@@ -80,28 +80,10 @@ class Tensor(Tensor_):
if isinstance(input_data, np_types):
input_data = np.array(input_data)

if input_data is not None and shape is not None:
raise ValueError("If input_data is available, shape doesn't need to be set")

if init is not None and (shape is None or dtype is None):
raise ValueError("init, dtype and shape must have values at the same time.")

if ((input_data is not None and init is None) or (input_data is None and init is not None)) is False:
raise TypeError("input_data and init can not be None at the same time.")

if isinstance(shape, numbers.Number):
shape = (shape,)

if input_data is not None:
if isinstance(input_data, np.ndarray) and input_data.ndim > 1 and input_data.size == 0:
raise ValueError("input_data can not contain zero dimension.")
if isinstance(input_data, (tuple, list)) and np.array(input_data).ndim > 1 \
and np.array(input_data).size == 0:
raise ValueError("input_data can not contain zero dimension.")

if shape is not None and not (hasattr(init, "__enable_zero_dim__") and init.__enable_zero_dim__):
if 0 in shape:
raise ValueError("Shape can not contain zero value.")
_check_tensor_input(input_data, dtype, shape, init)

# If input_data is tuple/list/numpy.ndarray, it's support in check_type method.
if init is None:
@@ -828,4 +810,26 @@ def _vm_compare(*args):
return Tensor(np.array(fn(y)))


def _check_tensor_input(input_data=None, dtype=None, shape=None, init=None):
"""Check the tensor input."""
if input_data is not None and shape is not None:
raise ValueError("If input_data is available, shape doesn't need to be set")

if init is not None and (shape is None or dtype is None):
raise ValueError("init, dtype and shape must have values at the same time.")

if (int(input_data is None) + int(init is None)) != 1:
raise TypeError("input_data and init can not be None at the same time.")

if input_data is not None:
if isinstance(input_data, np.ndarray) and input_data.ndim > 1 and input_data.size == 0:
raise ValueError("input_data can not contain zero dimension.")
if isinstance(input_data, (tuple, list)) and np.array(input_data).ndim > 1 \
and np.array(input_data).size == 0:
raise ValueError("input_data can not contain zero dimension.")

if shape is not None and not (hasattr(init, "__enable_zero_dim__") and init.__enable_zero_dim__) and 0 in shape:
raise ValueError("Shape can not contain zero value.")


tensor_operator_registry.register('vm_compare', _vm_compare)

+ 19
- 16
mindspore/train/serialization.py View File

@@ -654,28 +654,17 @@ def _export(net, file_name, file_format, *inputs):
def _save_mindir(net, file_name, *inputs):
"""Save MindIR format file."""
model = mindir_model()
if net._auto_parallel_mode:
phase_name = "predict"
else:
phase_name = 'export.mindir'

phase_name = "predict" if net._auto_parallel_mode else "export.mindir"

graph_id, _ = _executor.compile(net, *inputs, phase=phase_name,
do_convert=False, auto_parallel_mode=net._auto_parallel_mode)
mindir_stream = _executor._get_func_graph_proto(net, graph_id, 'mind_ir')

net_dict = net.parameters_dict()
data_total = 0
save_together = True

model.ParseFromString(mindir_stream)
for param_proto in model.graph.parameter:
name = param_proto.name[param_proto.name.find(":") + 1:]
if name in net_dict.keys():
data_total += sys.getsizeof(net_dict[name].data.asnumpy().tobytes()) / 1024
else:
raise RuntimeError('Graph parameter: {} Undefined in network.'.format(param_proto.name))
if data_total > TOTAL_SAVE:
save_together = False
break

save_together = _mindir_save_together(net_dict, model)

if save_together:
for param_proto in model.graph.parameter:
@@ -746,6 +735,20 @@ def _save_mindir(net, file_name, *inputs):
f.write(model.SerializeToString())


def _mindir_save_together(net_dict, model):
"""Whether graph and parameter save together during save mindir model."""
data_total = 0
for param_proto in model.graph.parameter:
name = param_proto.name[param_proto.name.find(":") + 1:]
if name in net_dict.keys():
data_total += sys.getsizeof(net_dict[name].data.asnumpy().tobytes()) / 1024
else:
raise RuntimeError('Graph parameter: {} Undefined in network.'.format(param_proto.name))
if data_total > TOTAL_SAVE:
return False
return True


def _quant_export(network, *inputs, file_format, **kwargs):
"""
Exports MindSpore quantization predict model to deploy with AIR and MINDIR.


+ 2
- 2
tests/ut/python/runtest.sh View File

@@ -13,9 +13,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
CURRPATH=$(cd "$(dirname $0)"; pwd)
CURRPATH=$(cd "$(dirname $0)" || exit; pwd)
IGNORE_EXEC="--ignore=$CURRPATH/exec"
PROJECT_PATH=$(cd ${CURRPATH}/../../..; pwd)
PROJECT_PATH=$(cd ${CURRPATH}/../../.. || exit; pwd)

if [ $BUILD_PATH ];then
echo "BUILD_PATH = $BUILD_PATH"


Loading…
Cancel
Save