开发者社区 > 云原生 > Serverless > 正文

函数计算FC的API调用出现过: 目前只要出图就报第3个错,怎么解决?

函数计算FC的API调用出现过: 目前只要出图就报第3个错,怎么解决?1、 {'error': 'AttributeError', 'detail': '', 'body': '', 'errors': 'change() and other events can only be called within a Blocks context.'}

2、{"error":"SafetensorError","detail":"","body":"","errors":"Error while deserializing header: MetadataIncompleteBuffer"}

3、{"error":"RuntimeError","detail":"","body":"","errors":"Expected all tensors to be on the same device, but found at least two devices, cpu and cuda:0! (when checking argument for argument index in method wrapper_CUDA__index_select)"}

展开
收起
三分钟热度的鱼 2023-08-22 17:55:40 134 0
2 条回答
写回答
取消 提交回答
  • 北京阿里云ACE会长

    我已经认真阅读了 你的问题:

    【 函数计算FC的API调用出现过: 目前只要出图就报第3个错,怎么解决?

    并思考了

    建议如下:


    第一个错误是一个 AttributeError,提示在调用change() 和其他事件时,只能在 Blocks 环境中调用。第二个错误是一个SafetensorError,提示在反序列化标头时出现 MetadataIncompleteBuffer 错误。
    针对第一个错误,您需要确保在调用change() 和其他事件时,确实存在 Blocks 环境中。请检查您的代码,确保在正确的位置使用了blocks.Block()创建了一个Block对象。例如:

    import torch
    from torch.nn import Module
    from torch.autograd import Variable
    class MyModel(Module):
    def init(self):
    super(MyModel, self).init()
    self.conv1 = ModuleList([Conv2d(1, 32, kernel_size=3, stride=1, padding=1)])
    self.conv2 = ModuleList([Conv2d(32, 64, kernel_size=3, stride=1, padding=1)])
    self.fc = ModuleList([Linear(64 8 8, 1024)])
    def forward(self, x):
    x = self.conv1(x)
    x = self.conv2(x)
    x = x.view(x.size(0), -1)
    x = self.fc(x)
    return x

    创建 Block 对象

    block = blocks.Block()

    将模型添加到 Block 中

    block.add_module(MyModel())

    使用 Block 调用 forward 函数

    result = block(Variable(torch.randn(1, 1, 224, 224)))
    复制复制

    针对第二个错误,您需要确保在反化序列头时,确实提供了足够的元数据。这可能涉及到调整您的序列化和反序列化代码。如果您使用的是 PyTorch 的 Tensor 类进行序列化,可以尝试增加“元数据”参数,以包含元数据。例如:

    import torch

    创建一个张量

    tensor = torch.randn(10, 20)

    序列化张量

    serialized_tensor = tensor.serialize(metadata=torch.Tensor.metadata())

    反序列化张量

    deserialized_tensor = torch.Tensor.from_serialized(serialized_tensor, metadata=serialized_tensor.metadata)
    复制复制

    该错误可能与函数计算的API的两个调用

    2023-08-26 17:59:13
    赞同 展开评论 打赏
  • 第三个有些是模型损坏了,可以试下关掉所有插件,换个内置模型请求下看看。此回答整理自钉群“【答疑群】函数计算 AIGC 场景技术交流”

    2023-08-22 18:25:47
    赞同 展开评论 打赏

快速交付实现商业价值。

相关产品

  • 函数计算
  • 相关电子书

    更多
    All in Serverless 阿里云核心产品全面升级 立即下载
    AIGC 浪潮之上,森马的 Serverless 实践之旅 立即下载
    极氪大数据 Serverless 应用实践 立即下载