# Buggy code
def _execute_map(cls, ctx, op):
    (data,), device_id, _ = as_same_device(
        [ctx[op.inputs[0].key]], device=op.device, ret_extra=True
    )
    index = ctx[op.inputs[1].key] if len(op.inputs) == 2 else None

    with device(device_id):
        if index is not None:
            # fetch the trained index
            trained_index = _load_index(ctx, op, index, device_id)
            return_index_type = _get_index_type(op.return_index_type, ctx)
            if return_index_type == "object":
                # clone a new one,
                # because faiss does not ensure thread-safe for operations that change index
                # https://github.com/facebookresearch/faiss/wiki/Threads-and-asynchronous-calls#thread-safety
                trained_index = faiss.clone_index(trained_index)
        else:
            trained_index = faiss.index_factory(
                data.shape[1], op.faiss_index, op.faiss_metric_type
            )
            if op.same_distribution:
                # no need to train, just create index
                pass
            else:
                # distribution no the same, train on each chunk
                trained_index.train(data)

            if device_id >= 0:  # pragma: no cover
                trained_index = _index_to_gpu(trained_index, device_id)
        if op.metric == "cosine":
            # faiss does not support cosine distances directly,
            # data needs to be normalize before adding to index,
            # refer to:
            # https://github.com/facebookresearch/faiss/wiki/FAQ#how-can-i-index-vectors-for-cosine-distance
            faiss.normalize_L2(data)

        # add data into index
        if device_id >= 0:  # pragma: no cover
            # gpu
            trained_index.add_c(data.shape[0], _swig_ptr_from_cupy_float32_array(data))
        else:
            trained_index.add(data)

        ctx[op.outputs[0].key] = _store_index(ctx, op, trained_index, device_id)


# Traceback (TypeError)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~/Workspace/mars/mars/serialize/pbserializer.pyx in mars.serialize.pbserializer.ProtobufSerializeProvider.serialize_field()
648             try:
--> 649                 self._set_tuple(value, field_obj, tp=field.type, weak_ref=field.weak_ref)
650             except TypeError:

~/Workspace/mars/mars/serialize/pbserializer.pyx in mars.serialize.pbserializer.ProtobufSerializeProvider._set_tuple()
364                     it_obj = Value()
--> 365                     self._set_value(val, it_obj, tp=tp.type if tp is not None else tp)
366                     res.append(it_obj)

~/Workspace/mars/mars/serialize/pbserializer.pyx in mars.serialize.pbserializer.ProtobufSerializeProvider._set_value()
559         else:
--> 560             cls._set_typed_value(value, obj, tp, weak_ref=weak_ref)
561

~/Workspace/mars/mars/serialize/pbserializer.pyx in mars.serialize.pbserializer.ProtobufSerializeProvider._set_typed_value()
443             value_field = PRIMITIVE_TYPE_TO_VALUE_FIELD[tp]
--> 444             setattr(obj, value_field, value)
445         elif tp is ValueType.slice:

TypeError: 10.0 has type float, but expected one of: int, long

The above exception was the direct cause of the following exception:

TypeError                                 Traceback (most recent call last)
/home/admin/work/public-mars-0.5.1.zip/mars/scheduler/graph.py in _execute_graph()

/home/admin/work/public-mars-0.5.1.zip/mars/utils.py in _wrapped()

/home/admin/work/public-mars-0.5.1.zip/mars/scheduler/graph.py in create_operand_actors()

/home/admin/work/public-mars-0.5.1.zip/mars/utils.py in _wrapped()

/home/admin/work/public-mars-0.5.1.zip/mars/scheduler/graph.py in get_executable_operand_dag()

/home/admin/work/public-mars-0.5.1.zip/mars/utils.py in serialize_graph()

~/Workspace/mars/mars/graph.pyx in mars.graph.DirectedGraph.to_pb()
420         return graph
--> 421
422     def to_pb(self, pb_obj=None, data_serial_type=None, pickle_protocol=None):

~/Workspace/mars/mars/serialize/core.pyx in mars.serialize.core.Serializable.to_pb()
686                                              pickle_protocol=pickle_protocol)
--> 687         return self.serialize(provider, obj=obj)
688

~/Workspace/mars/mars/serialize/core.pyx in mars.serialize.core.Serializable.serialize()
669     def serialize(self, Provider provider, obj=None):
--> 670         return provider.serialize_model(self, obj=obj)
671

~/Workspace/mars/mars/serialize/core.pyx in mars.serialize.core.Provider.serialize_model()
797         for name, field in model_instance._FIELDS.items():
--> 798             field.serialize(self, model_instance, obj)
799

~/Workspace/mars/mars/serialize/core.pyx in mars.serialize.core.Field.serialize()
154
--> 155     cpdef serialize(self, Provider provider, model_instance, obj):
156         return provider.serialize_field(self, model_instance, obj)

~/Workspace/mars/mars/serialize/core.pyx in mars.serialize.core.Field.serialize()
155     cpdef serialize(self, Provider provider, model_instance, obj):
--> 156         return provider.serialize_field(self, model_instance, obj)
157

~/Workspace/mars/mars/serialize/pbserializer.pyx in mars.serialize.pbserializer.ProtobufSerializeProvider.serialize_field()
630                     if val is not None:
--> 631                         self._serial_reference_value(tag, field.type.type.model, val, it_obj)
632                     elif isinstance(it_obj, Value):

~/Workspace/mars/mars/serialize/pbserializer.pyx in mars.serialize.pbserializer.ProtobufSerializeProvider._serial_reference_value()
572                 field_obj = value.cls(self)()
--> 573                 value.serialize(self, obj=field_obj)
574                 value_pb.type_id = value.__serializable_index__

~/Workspace/mars/mars/serialize/core.pyx in mars.serialize.core.Serializable.serialize()
669     def serialize(self, Provider provider, obj=None):
--> 670         return provider.serialize_model(self, obj=obj)
671

~/Workspace/mars/mars/serialize/core.pyx in mars.serialize.core.Provider.serialize_model()
797         for name, field in model_instance._FIELDS.items():
--> 798             field.serialize(self, model_instance, obj)
799

~/Workspace/mars/mars/serialize/core.pyx in mars.serialize.core.Field.serialize()
154
--> 155     cpdef serialize(self, Provider provider, model_instance, obj):
156         return provider.serialize_field(self, model_instance, obj)

~/Workspace/mars/mars/serialize/core.pyx in mars.serialize.core.Field.serialize()
155     cpdef serialize(self, Provider provider, model_instance, obj):
--> 156         return provider.serialize_field(self, model_instance, obj)
157

~/Workspace/mars/mars/serialize/pbserializer.pyx in mars.serialize.pbserializer.ProtobufSerializeProvider.serialize_field()
651                 exc_info = sys.exc_info()
--> 652                 raise TypeError(f'Failed to set field `{tag}` for {model_instance} with '
653                                 f'value {value}, reason: {exc_info[1]}') \

~/Workspace/mars/mars/serialize/pbserializer.pyx in mars.serialize.pbserializer.ProtobufSerializeProvider.serialize_field()
648             try:
--> 649                 self._set_tuple(value, field_obj, tp=field.type, weak_ref=field.weak_ref)
650             except TypeError:

~/Workspace/mars/mars/serialize/pbserializer.pyx in mars.serialize.pbserializer.ProtobufSerializeProvider._set_tuple()
364                     it_obj = Value()
--> 365                     self._set_value(val, it_obj, tp=tp.type if tp is not None else tp)
366                     res.append(it_obj)

~/Workspace/mars/mars/serialize/pbserializer.pyx in mars.serialize.pbserializer.ProtobufSerializeProvider._set_value()
559         else:
--> 560             cls._set_typed_value(value, obj, tp, weak_ref=weak_ref)
561

~/Workspace/mars/mars/serialize/pbserializer.pyx in mars.serialize.pbserializer.ProtobufSerializeProvider._set_typed_value()
443             value_field = PRIMITIVE_TYPE_TO_VALUE_FIELD[tp]
--> 444             setattr(obj, value_field, value)
445         elif tp is ValueType.slice:

TypeError: Failed to set field `shape` for Chunk <op=TensorTranspose, key=6fd87469bf886695950bdfb3164a7ce7> with value (10.0, -1), reason: 10.0 has type float, but expected one of: int, long

# Fixed code
def _execute_map(cls, ctx, op):
    (data,), device_id, xp = as_same_device(
        [ctx[op.inputs[0].key]], device=op.device, ret_extra=True
    )
    index = ctx[op.inputs[1].key] if len(op.inputs) == 2 else None

    with device(device_id):
        data = xp.ascontiguousarray(data)
        if index is not None:
            # fetch the trained index
            trained_index = _load_index(ctx, op, index, device_id)
            return_index_type = _get_index_type(op.return_index_type, ctx)
            if return_index_type == "object":
                # clone a new one,
                # because faiss does not ensure thread-safe for operations that change index
                # https://github.com/facebookresearch/faiss/wiki/Threads-and-asynchronous-calls#thread-safety
                trained_index = faiss.clone_index(trained_index)
        else:
            trained_index = faiss.index_factory(
                data.shape[1], op.faiss_index, op.faiss_metric_type
            )
            if op.same_distribution:
                # no need to train, just create index
                pass
            else:
                # distribution no the same, train on each chunk
                trained_index.train(data)

            if device_id >= 0:  # pragma: no cover
                trained_index = _index_to_gpu(trained_index, device_id)
        if op.metric == "cosine":
            # faiss does not support cosine distances directly,
            # data needs to be normalize before adding to index,
            # refer to:
            # https://github.com/facebookresearch/faiss/wiki/FAQ#how-can-i-index-vectors-for-cosine-distance
            faiss.normalize_L2(data)

        # add data into index
        if device_id >= 0:  # pragma: no cover
            # gpu
            trained_index.add_c(data.shape[0], _swig_ptr_from_cupy_float32_array(data))
        else:
            trained_index.add(data)

        ctx[op.outputs[0].key] = _store_index(ctx, op, trained_index, device_id)