折腾笔记[39]-使用Scala3的Storch计算

news/2025/12/8 0:38:26/文章来源:https://www.cnblogs.com/qsbye/p/19319227

摘要

使用Scala3的Storch(对标PyTorch)简单计算张量.

前言

本文目的是分享人工踩坑经验, AI搜索引擎可以更快给出正确结果(用于投喂AI😂).

简介

bytedeco系列javacpp库

[https://github.com/bytedeco]
在 AI 产业里,Java 平台主要承担“生产部署”的角色。因此,让 Java 能够一键调用深度模型部署所需的全部工具与框架,就成了顺理成章的需求。然而,最常用的框架之一 Apache TVM,过去对 Java 并不友好——直到最近 JavaCPP Presets for TVM 出现,才补齐了这块短板。

不过,TVM 的 GitHub 仓库里仍有超过 50% 的代码是 Python。这意味着,大多数场景下,部署链路里免不了要跑几段 Python 模块;再加上预处理脚本往往只有 Python 实现。与其硬搬,不如让 Java 直接无痛调用这些 Python 代码。于是,JavaCPP Presets for CPython、LLVM、MKL、oneDNN(DNNL/MKL-DNN)、OpenBLAS、NumPy、SciPy 应运而生。它们被打包成普通 JAR,像依赖任何第三方库一样直接引入;同时,这些 JAR 也能自动满足 TVM 的全部原生依赖——无需本地安装任何组件!

例如,在下面的 OpenCV 示例项目里,我们把所有依赖(含 Python 运行时)一并打包进 GraalVM Native Image,配合 Quarkus 即可“开箱即用”:
[https://github.com/bytedeco/sample-projects/tree/master/opencv-stitching-native]

In the AI industry, the Java platform is used mainly when deploying to production. Consequently, it makes sense to provide easy access to all the tools and frameworks necessary to deploy deep learning models for Java. However, one of the most widely used frameworks, Apache TVM, did not have very good support for Java, until recently. With the JavaCPP Presets for TVM, we now have proper support for the Java platform.

Still, according to the GitHub repository of TVM, over 50% of the code is written in Python. This means that, for most use cases, we probably need to run some of those modules as part of the deployment pipeline. Moreover, there are usually preprocessing algorithms among other things that have been implemented only as Python scripts. For these reasons, we might as well make it easy as possible to execute this Python code from Java. This is where the JavaCPP Presets for CPython, LLVM, MKL, oneDNN (aka DNNL, MKL-DNN), OpenBLAS, NumPy, and SciPy come in. They are available as normal JAR files, which can be used as any other JAR files, but they can also be used to satisfy the dependencies of TVM—without having to install anything! For example, as shown in the following sample project for OpenCV, we can bundle everything with GraalVM Native Image as usual and it simply just works as expected with Quarkus:
[https://github.com/bytedeco/sample-projects/tree/master/opencv-stitching-native]

<dependency><groupId>org.bytedeco</groupId><artifactId>javacpp-platform</artifactId><version>1.5.11</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>opencv-platform</artifactId><version>4.10.0-1.5.11</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>ffmpeg-platform</artifactId><version>7.1-1.5.11</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>flycapture-platform</artifactId><version>2.13.3.31-1.5.9</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>spinnaker-platform</artifactId><version>4.0.0.116-1.5.11</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>libdc1394-platform</artifactId><version>2.2.6-1.5.9</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>libfreenect-platform</artifactId><version>0.5.7-1.5.9</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>libfreenect2-platform</artifactId><version>0.2.0-1.5.9</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>librealsense-platform</artifactId><version>1.12.4-1.5.9</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>librealsense2-platform</artifactId><version>2.53.1-1.5.9</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>videoinput-platform</artifactId><version>0.200-1.5.9</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>artoolkitplus-platform</artifactId><version>2.3.1-1.5.9</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>chilitags-platform</artifactId><version>master-1.5.8</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>flandmark-platform</artifactId><version>1.07-1.5.8</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>arrow-platform</artifactId><version>6.0.1-1.5.8</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>hdf5-platform</artifactId><version>1.14.3-1.5.10</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>hyperscan-platform</artifactId><version>5.4.2-1.5.10</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>lz4-platform</artifactId><version>1.9.4-1.5.8</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>mkl-platform</artifactId><version>2025.0-1.5.11</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>mkl-dnn-platform</artifactId><version>0.21.5-1.5.8</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>dnnl-platform</artifactId><version>3.6.1-1.5.11</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>openblas-platform</artifactId><version>0.3.28-1.5.11</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>arpack-ng-platform</artifactId><version>3.9.1-1.5.11</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>cminpack-platform</artifactId><version>1.3.11-1.5.11</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>fftw-platform</artifactId><version>3.3.10-1.5.11</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>gsl-platform</artifactId><version>2.8-1.5.11</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>cpython-platform</artifactId><version>3.13.0-1.5.11</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>numpy-platform</artifactId><version>2.1.3-1.5.11</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>scipy-platform</artifactId><version>1.14.1-1.5.11</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>gym-platform</artifactId><version>0.26.2-1.5.8</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>llvm-platform</artifactId><version>19.1.3-1.5.11</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>libffi-platform</artifactId><version>3.4.6-1.5.11</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>libpostal-platform</artifactId><version>1.1-1.5.8</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>libraw-platform</artifactId><version>0.21.2-1.5.11</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>leptonica-platform</artifactId><version>1.85.0-1.5.11</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>tesseract-platform</artifactId><version>5.5.0-1.5.11</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>caffe-platform</artifactId><version>1.0-1.5.8</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>openpose-platform</artifactId><version>1.7.0-1.5.8</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>cuda-platform</artifactId><version>12.6-9.5-1.5.11</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>nvcodec-platform</artifactId><version>12.2.72-1.5.11</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>opencl-platform</artifactId><version>3.0-1.5.11</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>mxnet-platform</artifactId><version>1.9.1-1.5.8</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>pytorch-platform</artifactId><version>2.5.1-1.5.11</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>sentencepiece-platform</artifactId><version>0.2.0-1.5.11</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>tensorflow-platform</artifactId><version>1.15.5-1.5.8</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>tensorflow-lite-platform</artifactId><version>2.18.0-1.5.11</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>tensorrt-platform</artifactId><version>10.6-1.5.11</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>tritonserver-platform</artifactId><version>2.51.0-1.5.11</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>ale-platform</artifactId><version>0.8.0-1.5.8</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>depthai-platform</artifactId><version>2.24.0-1.5.10</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>onnx-platform</artifactId><version>1.17.0-1.5.11</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>ngraph-platform</artifactId><version>0.26.0-1.5.8</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>onnxruntime-platform</artifactId><version>1.20.0-1.5.11</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>tvm-platform</artifactId><version>0.18.0-1.5.11</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>bullet-platform</artifactId><version>3.25-1.5.11</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>liquidfun-platform</artifactId><version>master-1.5.8</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>qt-platform</artifactId><version>5.15.2-1.5.8</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>skia-platform</artifactId><version>2.88.3-1.5.8</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>cpu_features-platform</artifactId><version>0.7.0-1.5.8</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>modsecurity-platform</artifactId><version>3.0.8-1.5.8</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>systems-platform</artifactId><version>1.5.11</version></dependency><!-- Additional dependencies required to use CUDA, cuDNN, NCCL, and TensorRT --><dependency><groupId>org.bytedeco</groupId><artifactId>opencv-platform-gpu</artifactId><version>4.10.0-1.5.11</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>caffe-platform-gpu</artifactId><version>1.0-1.5.8</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>mxnet-platform-gpu</artifactId><version>1.9.1-1.5.8</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>pytorch-platform-gpu</artifactId><version>2.5.1-1.5.11</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>tensorflow-platform-gpu</artifactId><version>1.15.5-1.5.8</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>onnxruntime-platform-gpu</artifactId><version>1.20.0-1.5.11</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>tvm-platform-gpu</artifactId><version>0.18.0-1.5.11</version></dependency><!-- Optional dependencies to load Python-enabled builds --><dependency><groupId>org.bytedeco</groupId><artifactId>tensorflow-platform-python</artifactId><version>1.15.5-1.5.8</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>tensorflow-platform-python-gpu</artifactId><version>1.15.5-1.5.8</version></dependency><!-- Additional dependencies to use bundled CUDA, cuDNN, NCCL, and TensorRT --><dependency><groupId>org.bytedeco</groupId><artifactId>cuda-platform-redist</artifactId><version>12.6-9.5-1.5.11</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>tensorrt-platform-redist</artifactId><version>10.6-1.5.11</version></dependency><!-- Additional dependencies to use bundled full version of MKL --><dependency><groupId>org.bytedeco</groupId><artifactId>mkl-platform-redist</artifactId><version>2025.0-1.5.11</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>javacv-platform</artifactId><version>1.5.11</version></dependency>

清单:

Prebuilt Java Bindings to C/C++ Libraries
These are part of a project that we call the JavaCPP Presets. Many coexist in the same GitHub repository, and all use JavaCPP to wrap predefined C/C++ libraries from open-source land. The bindings expose almost all of the relevant APIs and make them available in a portable and user-friendly fashion to any Java virtual machine (including Android), as if they were like any other normal Java libraries. We have presets for the following C/C++ libraries:
OpenCV – [sample usage] [API] – More than 2500 optimized computer vision and machine learning algorithms
FFmpeg – [sample usage] [API] – A complete, cross-platform solution to record, convert and stream audio and video
FlyCapture – [sample usage] [API] – Image acquisition and camera control software from PGR
Spinnaker – [sample usage] [API] – Image acquisition and camera control software from FLIR
libdc1394 – [sample usage] [API] – A high-level API for DCAM/IIDC cameras
OpenKinect – [sample usage] [API] [API 2] – Open source library to use Kinect for Xbox and for Windows sensors
librealsense – [sample usage] [API] [API 2] – Cross-platform library for Intel RealSense depth and tracking cameras
videoInput – [sample usage] [API] – A free Windows video capture library
ARToolKitPlus – [sample usage] [API] – Marker-based augmented reality tracking library
Chilitags – [sample usage] [API] – Robust fiducial markers for augmented reality and robotics
flandmark – [sample usage] [API] – Open-source implementation of facial landmark detector
Arrow – [sample usage] [API] – A cross-language development platform for in-memory data
HDF5 – [sample usage] [API] – Makes possible the management of extremely large and complex data collections
Hyperscan – [sample usage] [API] – High-performance regular expression matching library
LZ4 – [sample usage] [API] – Extremely fast compression algorithm
MKL – [sample usage] [API] – The fastest and most-used math library for Intel-based systems
oneDNN – [sample usage] [API] [API 2] – Intel Math Kernel Library for Deep Neural Networks (DNNL)
OpenBLAS – [sample usage] [API] – An optimized BLAS library based on GotoBLAS2 1.13 BSD version, plus LAPACK
ARPACK-NG – [sample usage] [API] – Collection of subroutines designed to solve large scale eigenvalue problems
CMINPACK – [sample usage] [API] – For solving nonlinear equations and nonlinear least squares problems
FFTW – [sample usage] [API] – Fast computing of the discrete Fourier transform (DFT) in one or more dimensions
GSL – [sample usage] [API] – The GNU Scientific Library, a numerical library for C and C++ programmers
CPython – [sample usage] [API] – The standard runtime of the Python programming language
NumPy – [sample usage] [API] – Base N-dimensional array package
SciPy – [sample usage] [API] – Fundamental library for scientific computing
Gym – [sample usage] [API] – A toolkit for developing and comparing reinforcement learning algorithms
LLVM – [sample usage] [API] – A collection of modular and reusable compiler and toolchain technologies
libffi – [sample usage] [API] – A portable foreign-function interface library
libpostal – [sample usage] [API] – For parsing/normalizing street addresses around the world
LibRaw – [sample usage] [API] – A simple and unified interface for RAW files generated by digital photo cameras
Leptonica – [sample usage] [API] – Software useful for image processing and image analysis applications
Tesseract – [sample usage] [API] – Probably the most accurate open source OCR engine available
Caffe – [sample usage] [API] – A fast open framework for deep learning
OpenPose – [sample usage] [API] – Real-time multi-person keypoint detection for body, face, hands, and foot estimation
CUDA – [sample usage] [API] – Arguably the most popular parallel computing platform for GPUs
NVIDIA Video Codec SDK – [sample usage] [API] – An API for hardware accelerated video encode and decode
OpenCL – [sample usage] [API] – Open standard for parallel programming of heterogeneous systems
MXNet – [sample usage] [API] – Flexible and efficient library for deep learning
PyTorch – [sample usage] [API] – Tensors and dynamic neural networks with strong GPU acceleration
SentencePiece – [sample usage] [API] – Unsupervised text tokenizer for neural-network-based text generation
TensorFlow – [sample usage] [API] – Computation using data flow graphs for scalable machine learning
TensorFlow Lite – [sample usage] [API] – An open source deep learning framework for on-device inference
TensorRT – [sample usage] [API] – High-performance deep learning inference optimizer and runtime
Triton Inference Server – [sample usage] [API] – An optimized cloud and edge inferencing solution
ALE – [sample usage] [API] – The Arcade Learning Environment to develop AI agents for Atari 2600 games
DepthAI – [sample usage] [API] – An embedded spatial AI platform built around Intel Myriad X
ONNX – [sample usage] [API] – Open Neural Network Exchange, an open source format for AI models
nGraph – [sample usage] [API] – An open source C++ library, compiler, and runtime for deep learning frameworks
ONNX Runtime – [sample usage] [API] – Cross-platform, high performance scoring engine for ML models
TVM – [sample usage] [API] – An end to end machine learning compiler framework for CPUs, GPUs and accelerators
Bullet Physics SDK – [sample usage] [API] – Real-time collision detection and multi-physics simulation
LiquidFun – [sample usage] [API] – 2D physics engine for games
Qt – [sample usage] [API] – A cross-platform framework that is usually used as a graphical toolkit
Skia – [sample usage] [API] – A complete 2D graphic library for drawing text, geometries, and images
cpu_features – [sample usage] [API] – A cross platform C99 library to get cpu features at runtime
ModSecurity – [sample usage] [API] – A cross platform web application firewall (WAF) engine for Apache, IIS and Nginx
Systems – [sample usage] [API] – To call native functions of operating systems (glibc, XNU libc, Win32, etc)
Add here your favorite C/C++ library, for example: Caffe2, OpenNI, OpenMesh, PCL, etc. Read about how to do that.

Scala3简介

[https://scala-lang.org.cn]
[https://scala-lang.org/]
[https://www.scala-js.org/]
一门与你共同成长、从几行脚本到跨平台大型应用的编程语言。
Scala 让你用更少代码,做更多事情。作为一门高级语言,它的现代特性显著提升生产力,让代码更易读。在 Scala 中,你可以将函数式与面向对象两种范式自由组合,优雅地组织程序结构。
凭借 JVM、JavaScript 与 Native 三大运行时,Scala 天生适合构建高速、并发、分布式系统。它把“互操作”放在首位,轻松调用无数经产业验证的库,坐拥庞大生态。
Scala 的静态类型系统默认帮你打造安全可靠的系统:智能的内置检查、可操作的报错信息、线程安全的数据结构与集合,让许多棘手 bug 在程序首次运行前就被消灭。

我该选哪个版本?
Scala Next 当前 3.7.4——默认推荐,功能最新、修复最全,适合大多数用户。
Scala LTS 当前 3.3.7——长期支持版,建议用于发布库。

安装 Scala,最省心的是用 cs setup(由 Coursier 驱动的官方安装器)。一条命令,即可装好使用最新 Scala 所需的一切工具。

A programming language that scales with you: from small scripts to large multiplatform applications.
Scala lets you write less to do more. As a high-level language, its modern features increase productivity and lead to more readable code. With Scala, you can combine both functional and object-oriented programming styles to help structure programs.
Scala is well suited to building fast, concurrent, and distributed systems with its JVM, JavaScript and Native runtimes. Scala prioritizes interoperability, giving easy access to many ecosystems of industry-proven libraries.
Scala's static types help you to build safe systems by default. Smart built-in checks and actionable error messages, combined with thread-safe data structures and collections, prevent many tricky bugs before the program first runs.
Which version of Scala should I choose?
Scala Next currently 3.7.4 - The default to be used by most users, containing the latest features, bug fixes and improvements.
Scala LTS currently 3.3.7 - Advised to be used for publishing libraries.
To install Scala, it is recommended to use cs setup, the Scala installer powered by Coursier. It installs everything necessary to use the latest Scala release from a command line.

Storch简介

[https://storch.dev/]
[https://github.com/bytedeco/pytorch]
[https://github.com/bytedeco/storch]
[https://github.com/mullerhai/storch-tutorial]
GPU 加速的深度学习与数值计算,专为 Scala 3 打造
STorch 是一个用于快速张量计算与深度学习的 Scala 库,功能对标 PyTorch。
与 PyTorch 一样,STorch 提供 📇 🏠 🪟 🍎 🐧
二者均从 C++ LibTorch 编译而来,STorch 与 PyTorch 平起平坐。
提供 NumPy 风格的张量操作 API
支持 GPU(未来将支持华为 CANN NPU)
在 Linux | macOS | Windows | iOS | Android 上均可自动求导
内置神经网络 API,轻松搭建与训练模型。
PyTorch 有的,STorch 全都有!!!
Python PyTorch 开发者可零成本迁移
大数据开发者与数据科学家的 ROI 最大化
支持 LLM | 推荐系统 | 计算机视觉 | NLP | 金融科技 | 强化学习 | 大数据 | 数据科学研究
庞大的深度学习生态链条,让你专注开发本身
支持 Java SIMD Vector API,请使用 JDK 17 及以上版本
可使用 NCCL | GLOO | RAY | SPARK | FLINK 进行分布式训练与推理(未来将支持 UCC | MPI)
是 Apache Spark | Flink | Polar 的最佳数据驱动流程与 Monad 特征工程管道伙伴
STorch 的 API 设计紧贴 Python 版 PyTorch,让已有模型的迁移和 PyTorch 用户的学习成本降到最低。

GPU accelerated deep learning and numeric computing for Scala 3.
STorch is a Scala library for fast tensor computations and deep learning, same as PyTorch.
Like PyTorch, STorch provides 📇 🏠 🪟 🍎 🐧
Both Compile from Cpp LibTorch, STorch has Same Equal Position with Pytorch
A NumPy like API for working with tensors
GPU support [Future will support HuaWei CANN NPU]
Automatic differentiation on Linux | Mac | Windows | IOS | Andriod
A neural network API for building and training neural networks.
What All you need Pytorch features could find in STorch !!!
Minimal time and learning cost migrate from python pytorch developer
Maximal ROI for Big Data developer and Data Scientist
Support LLM | Recommend System | Compute Vision | NLP | Financial Tech | RL |Big Data | Data Science Research
Big Deep Learning Environment Chains boost you concentrate on development
Support Java SMID Vector API, Please use JDK Version >= 17
Could Use NCCL | GLOO | RAY | SPARK | FLINK for DISTRIBUTE TRAINING and inference [Future will support UCC | MPI]
Best Data Driven Process and Monad Feature Engineer Model Pipeline Partner for Apache Spark | Flink | Polar
STorch aims to close to the Python API to make porting existing models and the life of people already familiar with PyTorch easier.

实现

1. 安装Scala3

  • Scala 3.7.2
# macOS
brew install coursier/formulas/coursier && cs setup# Linux
curl -fL https://github.com/coursier/coursier/releases/latest/download/cs-x86_64-pc-linux.gz | gzip -d > cs && chmod +x cs && ./cs setup
curl -fL https://github.com/VirtusLab/coursier-m1/releases/latest/download/cs-aarch64-pc-linux.gz | gzip -d > cs && chmod +x cs && ./cs setup# Windows
# 使用安装包: [https://github.com/coursier/coursier/releases/latest/download/cs-x86_64-pc-win32.zip]

2. 手动编译STorch的jar包

  • 原因: io.github.mullerhai链接重定向有问题
# 这个版本更新一点
git clone https://github.com/bytedeco/pytorch.git
cd pytorch
sbt publishLocal

3. 构建工程

初始化工程:

sbt new scala/scala3.g8

build.sbt

val scala3Version = "3.7.2"lazy val root = project.in(file(".")).settings(name         := "hello_storch",version      := "0.1.0-SNAPSHOT",scalaVersion := scala3Version,/* ---------- 仓库配置 ---------- */resolvers ++= Seq(Resolver.mavenCentral,Resolver.sonatypeCentralSnapshots,// 存放Storch仓库jar包Resolver.file("local", file(System.getProperty("user.home") + "/.ivy2/local"))(Resolver.ivyStylePatterns)),/* ---------- 3. 依赖 ---------- */libraryDependencies ++= Seq("io.github.mullerhai" % "storch_core_3"  % "0.7.6-1.5.12","org.bytedeco"        % "pytorch-platform" % "2.5.1-1.5.11","org.scalameta"       %% "munit"         % "1.0.0" % Test),/* 原生库需要 fork */fork := true,/* 设置编码以支持中文 */javaOptions += "-Dfile.encoding=UTF-8",Compile / compile / javacOptions ++= Seq("-encoding", "UTF-8"),Compile / scalacOptions ++= Seq("-encoding", "UTF-8"),/* 设置环境变量以支持中文输出 */envVars := Map("JAVA_TOOL_OPTIONS" -> "-Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8","file.encoding" -> "UTF-8","sun.jnu.encoding" -> "UTF-8","sun.stdout.encoding" -> "UTF-8","sun.stderr.encoding" -> "UTF-8"),/* 确保控制台输出使用正确编码 */run / javaOptions ++= Seq("-Dfile.encoding=UTF-8","-Dsun.stdout.encoding=UTF-8", "-Dsun.stderr.encoding=UTF-8"))

src/main/scala/Main.scala

import torch._
import torch.ops._@main def hello(): Unit =println("Hello world!")// 创建张量的基本示例println("=== 创建张量示例 ===")// 从序列创建张量val data = Seq(0, 1, 2, 3)// data: Seq[Int] = List(0, 1, 2, 3)val t1 = Tensor(data)// t1: Tensor[Int32] = dtype=int32, shape=[4], device=CPU // [0, 1, 2, 3]println(s"t1: $t1")// 检查张量是否等于arange生成的张量val arangeTensor = arange(0, 4, dtype = int32)println(s"t1.equal(arange(0,4)): ${t1.equal(arangeTensor)}")// 数据类型转换val t2 = t1.to(dtype = float32)// t2: Tensor[Float32] = dtype=float32, shape=[4], device=CPU // [0.0000, 1.0000, 2.0000, 3.0000]println(s"t2 (转换为float32): $t2")// 张量加法val t3 = t1 + t2// t3: Tensor[Float32] = dtype=float32, shape=[4], device=CPU // [0.0000, 2.0000, 4.0000, 6.0000]println(s"t3 = t1 + t2: $t3")println("\n=== 随机张量和特殊张量 ===")// 创建随机张量val shape = Seq(2, 3)// shape: Seq[Int] = List(2, 3)val randTensor = rand(shape)// randTensor: Tensor[Float32] = dtype=float32, shape=[2, 3], device=CPU // [[0.4341, 0.9738, 0.9305],//  [0.8987, 0.1122, 0.3912]]println(s"随机张量: $randTensor")// 创建零张量val zerosTensor = zeros(shape, dtype = int64)// zerosTensor: Tensor[Int64] = dtype=int64, shape=[2, 3], device=CPU // [[0, 0, 0],//  [0, 0, 0]]println(s"零张量: $zerosTensor")println("\n=== 简单神经网络示例 ===")// 创建输入张量val x = ones(Seq(5))// x: Tensor[Float32] = dtype=float32, shape=[5], device=CPU // [1.0000, 1.0000, 1.0000, 1.0000, 1.0000]println(s"输入x: $x")// 创建权重矩阵(需要梯度)val w = randn(Seq(5, 3), dtype = float32, requires_grad = true)// w: Tensor[Float32] = dtype=float32, shape=[5, 3], device=CPU // 随机初始化的权重矩阵println(s"权重矩阵w: $w")// 创建偏置向量(需要梯度)val b = randn(Seq(3), dtype = float32, requires_grad = true)// b: Tensor[Float32] = dtype=float32, shape=[3], device=CPU // 随机初始化的偏置println(s"偏置b: $b")// 前向传播计算val z = (x.matmul(w)) + b// z: Tensor[Float32] = dtype=float32, shape=[3], device=CPU // 线性变换结果println(s"前向传播结果z = x·w + b: $z")println("\nStorch 张量操作演示完成!")

编译:

sbt update
sbt compile
sbt run

4. 效果

现在中文可以正常显示了:=== 创建张量示例 ===t2 (转换为float32): tensor dtype=float32, shape=[4], device=CPU=== 随机张量和特殊张量 ===随机张量: tensor dtype=float32, shape=[2, 3], device=CPU零张量: tensor dtype=int64, shape=[2, 3], device=CPU=== 简单神经网络示例 ===输入x: tensor dtype=float32, shape=[5], device=CPU权重矩阵w: tensor dtype=float32, shape=[5, 3], device=CPU偏置b: tensor dtype=float32, shape=[3], device=CPU前向传播结果z = x·w + b: tensor dtype=float32, shape=[3], device=CPUStorch 张量操作演示完成!

5. 提示

如果遇到报错, 可以使用例如kimi-cli/gemini-cli/claude-cli等AI工具自动处理错误.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/991952.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

day03 指针应用和文件操作

C语言指针的详解与应用指针存放的是首地址,指针是变量关于指针 p++ 的操作#include <stdio.h> int main() {char a = 0x66;char *p; // 星号通常和变量名放在一起,防止歧义p = &a; // 把a的地址给pprin…

ZenMux 企业级大模型聚合平台,免费试用模型 Gemini 3 Pro

ZenMux 是全球首个支持保险赔付机制的企业级大模型聚合平台。 ZenMux 聚合了全球领先的闭源和开源大语言模型,在一个统一的平台上为开发者提供便捷的模型调用服务。 ZenMux 提供统一的 API 接口访问 OpenAI、Anthropi…

102302139 尚子骐 数据采集与融合作业4

作业一1. 完整代码及运行结果点击查看代码 from selenium import webdriver from selenium.webdriver.edge.service import Service from selenium.webdriver.common.by import By from selenium.webdriver.support.ui…

代码随想录32_动态规划基础

代码随想录32_动态规划基础理论基础 Dynamic Programming,当前状态由上一状态推导而来。 FIB 斐波那契 1.初始化数组的时候需要分配数组大小; 2.如果没有分配,使用push_back是安全的; 题解 class Solution { public…

vsc_backgroud_css小记

突然想给vsc背景加张图片 下载了 Custom CSS and JS Loader的插件 这个插件专门用来加载自定义的css和js文件 然后用ai写了个css文件 /* VS Code背景图:左下角局部显示 + 低存在感 */ body {/* https://img2024.cnblo…

3、缺陷管理

3.1缺陷介绍 1、缺陷的定义 软件在使用过程中存在的任何问题都叫软件的缺陷,简称bug2、缺陷的判定标准软件为实现需求(规格)说明书中明确要求的功能——少功能 软件出现了需求(规格)说明书中指明不应该出现的错误…

SGLang 的 DP Attention 模式浅析 - -银光

SGLang 的 DP Attention 模式浅析注:本文已于2025.11.30 发表于知乎和公众号 1. 简介 前序的三篇笔记,先系统总结各种SGLang 分布式集群模式,然后对TP 集群的完整执行流程做解析,再重点介绍 PP 集群的任务调度和分…

记我第一次代码审计 (bluecmsv1.6的sql注入复现)

此次漏洞复现搭建环境:php5.4.6+phpstudy+windows 1.首先获取bluecms源码并导入到phpstorm方便后续的代码审计,定位漏洞代码 在phpstorm可以使用快捷键shift+ctrl+f使用全局搜索功能并且支持正则匹配,通过这段查询语…

每日3题 2(暂鸽)

事情实在太多了先鸽着,在学莫比乌斯反演

K8S的Service

原文博客:https://nosae.topapiVersion: discovery.k8s.io/v1 kind: EndpointSlice metadata: name: my-service-1 # EndpointSlice命令的最佳实践是以svc的名称作为前缀 labels: # 这个label的值必须是svc的名称,将…

在MacOS中运行k3s

原文博客:https://nosae.topk3shttps://github.com/caicloud/kube-ladder安装ubuntu家的multipass虚拟机 brew install multipass(镜像路径在/var/root/Library/Caches/multipassd/qemu/vault/images下) 创建一个虚拟…

2025 最新成都/西南地区品牌策划服务商 / 公司 TOP5 评测!实战案例 + 系统服务权威榜单发布,助力企业品牌资产与业绩双增长

随着市场竞争的日益激烈,专业的品牌策划已成为企业打造核心竞争力、实现可持续发展的关键。本榜单基于技术实力、行业经验、服务案例、客户口碑四大维度,结合行业协会数据及市场反馈,权威解析2025年五大品牌策划公司…

第48天(中等题 数据结构)

打卡第四十八天 2道中等题题目:思路:前缀和+贪心,一边遍历数组计算前缀和,一边维护前缀和的最小值(相当于股票最低价格),用当前的前缀和(卖出价格)减去前缀和的最小值(买入价格),就得到了以当前元素结尾的…

2025杭州有哪些靠谱的舞蹈培训机构:拱墅区舞蹈培训机构推荐

2025杭州有哪些靠谱的舞蹈培训机构:拱墅区舞蹈培训机构推荐!选择舞蹈培训机构时,建议从以下几个维度进行考量:教学体系:关注课程设置的系统性和科学性,是否形成循序渐进的教学路径师资队伍:了解教师的专业背景、持…

2025包装机械厂家/粉末吨袋包装机厂家综合实力榜单

2025包装机械厂家/粉末吨袋包装机厂家综合实力榜单。粉末吨袋包装机作为大宗粉末物料包装的核心设备,凭借高效、精准、环保的特性,成为化工、建材、粮食等行业的关键装备。其核心优势在于能针对性解决粉末易飞扬、流…

为什么使用 telnet 命令可以探测目标主机的某个端口是否开放?

为什么使用 telnet 命令可以探测目标主机的某个端口是否开放?Telnet 属于应用层协议,传输层采用 TCP,服务器默认监听 23 端口,通过 telnet 协议可以实现远程登录设备,并以命令方式进行交互。 使用 Telnet 传输数据…

2025成都/西南地区营销策划服务商 TOP5 评测!实战案例驱动 + 系统服务权威榜单发布,赋能品牌资产与业绩双增长

随着市场竞争的日益激烈,专业的营销策划已成为企业打造品牌、提升销量的关键。本榜单基于技术实力、行业适配性、服务效能、实战案例以及行业贡献五大维度,结合众多企业的实际反馈与行业数据,权威解析2025年五大营销…

PDFsharp:强大的 .NET 跨平台 PDF 处理库

PDFsharp:强大的 .NET 跨平台 PDF 处理库Posted on 2025-12-08 00:00 lzhdim 阅读(0) 评论(0) 收藏 举报一、简介 PDFsharp 是一个功能强大且免费开源的 .NET 库,专为创建、修改和处理 PDF 文档而设计。它支持多…

2025 成都/西南地区品牌定位服务商 TOP5 评测!实战案例驱动+系统化战略权威榜单发布,助力企业实现品牌资产与业绩双增长

随着市场竞争加剧,精准的品牌定位成为企业突破同质化困境、赢得消费者心智的关键。本榜单基于战略系统性、行业适配性、实战效果、资质荣誉四大维度,结合西南地区品牌服务市场深度调研,权威解析2025年五大品牌定位服…