百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
C

cumo

> DevOps
开源

Cumo(发音为"koomo")是CUDA意识数字库,其界面与Ruby Numo高度相容

99 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

Cumo(发音为"koomo")是CUDA意识数字库,其界面与Ruby Numo高度相容

Cumo

Cumo (pronounced "koomo") is a CUDA-aware, GPU-optimized numerical library that offers a significant performance boost over Ruby Numo, while (mostly) maintaining drop-in compatibility.

Requirements

  • Ruby 3.0 or later
  • NVIDIA GPU Compute Capability 3.5 (Kepler) or later
  • CUDA 11.0 or later
  • cuDNN 8.0 or later (optional, for the cuDNN features)

Preparation

Install CUDA and set your environment variables as follows:

bash
export CUDA_PATH="/usr/local/cuda"
export CPATH="$CUDA_PATH/include:$CPATH"
export LD_LIBRARY_PATH="$CUDA_PATH/lib64:$CUDA_PATH/lib:$LD_LIBRARY_PATH"
export PATH="$CUDA_PATH/bin:$PATH"
export LIBRARY_PATH="$CUDA_PATH/lib64:$CUDA_PATH/lib:$LIBRARY_PATH"

To use cuDNN features, install cuDNN and set your environment variables as follows:

bash
export CUDNN_ROOT_DIR=/path/to/cudnn
export CPATH=$CUDNN_ROOT_DIR/include:$CPATH
export LD_LIBRARY_PATH=$CUDNN_ROOT_DIR/lib64:$LD_LIBRARY_PATH
export LIBRARY_PATH=$CUDNN_ROOT_DIR/lib64:$LIBRARY_PATH

FYI: I use cudnnenv to install cudnn under my home directory like export CUDNN_ROOT_DIR=/home/sonots/.cudnn/active/cuda.

Installation

Add the following line to your Gemfile:

ruby
gem 'cumo'

And then execute:

$ bundle

Or install it yourself as:

$ gem install cumo

How To Use

Quick start

An example:

ruby
[1] pry(main)> require "cumo/narray"
=> true
[2] pry(main)> a = Cumo::DFloat.new(3,5).seq
=> Cumo::DFloat#shape=[3,5]
[[0, 1, 2, 3, 4],
 [5, 6, 7, 8, 9],
 [10, 11, 12, 13, 14]]
[3] pry(main)> a.shape
=> [3, 5]
[4] pry(main)> a.ndim
=> 2
[5] pry(main)> a.class
=> Cumo::DFloat
[6] pry(main)> a.size
=> 15

Switching from Numo to Cumo

The following find-and-replace should just work:

find . -type f | xargs sed -i -e 's/Numo/Cumo/g' -e 's/numo/cumo/g'

If you want to dynamically switch between Numo and Cumo, something like the following will work:

ruby
if gpu
  require 'cumo/narray'
  xm = Cumo
else
  require 'numo/narray'
  xm = Numo
end

a = xm::DFloat.new(3,5).seq

Incompatibility With Numo

Numo returns a Ruby numeric object wherever a result is 0-dimensional, while Cumo returns the 0-dimensional NArray itself. Cumo differs in this way to avoid synchronization and minimize CPU ⇄ GPU data transfer. That is not only a cost of the port; see Keeping Scalars On The Device and Ruby Floats In NMath Promote To Double for what it buys.

The methods affected are:

  • [] and extract
  • count_true and count_false
  • reductions down to a single value: sum, prod, mean, stddev, var, rms, min, max, ptp, minmax, median, mulsum, dot, inner
  • index reductions: max_index, min_index, argmax, argmin

A 0-dimensional Cumo::Bit is truthy even when it holds 0, because Ruby treats every object but nil and false as true. Comparing two scalars therefore takes the wrong branch without raising anything:

ruby
a = Cumo::SFloat[5.0]
a[0]  Cumo::Bit#shape=[] holding 0
(a[0]  :yes, where Numo gives :no

`assert_operator(a[0], : true Cumo.disable_compatible_mode # disable Cumo.compatible_mode_enabled? #=> false


You can also use the following methods which behave like Numo's NArray methods. The behavior of these methods does not depend on `compatible_mode`.

* `extract_cpu`
* `aref_cpu(*idx)`
* `count_true_cpu`
* `count_false_cpu`

```ruby
a.aref_cpu(0)  false in either mode
Float(a.sum)          #=> 7.0 in either mode

…


scalars        convergence test    us/iter    readbacks/iter
Ruby Floats    every iteration       136.7        2.02
Ruby Floats    never                 134.4        2.02
0-dim NArray   every iteration       142.6        1.02
0-dim NArray   every 20th             65.6        0.06
0-dim NArray   never                  60.3        0.02

Written with Ruby Floats the loop reads back twice an iteration whatever the convergence test does, since alpha needs pap and beta needs rs_new as Floats. Thinning the test cannot get under that floor, and keeping the scalars as 0-dimensional NArrays buys nothing on its own. The two only pay together, and together they are worth 2.1x. The relative residual is identical in every row.

ruby
alpha = rs_old / pap   # a 0-dimensional NArray, divided on the device
x += p_dir * alpha     # and consumed there, without crossing the bus

Read the value back once the loop is done, or every k iterations if it has to test something.

Reshape Copies, Reshape! Does Not

reshape answers a copy of the whole array, never a view. Slicing answers a view, so the two look alike and are not:

ruby
a = Cumo::SFloat.new(4, 6).seq
a[0..1, true][0, 0] = 77.0   # a view, so a changes
a.reshape(2, 12)[0, 0] = 99.0  # a copy, so a does not

This is what Numo does too, and numpy is where the expectation comes from: there reshape answers a view whenever the strides allow one. On a GPU the difference is an allocation and a copy kernel, paid every call. reshape! changes the receiver in place and costs neither. RTX 5070 Ti Laptop, Cumo::SFloat, 200 calls a measurement:

shape        reshape     reshape!    the copy allocates
1x768        5.1 us      0.04 us          3 KB
1024x768    39.1 us      0.18 us          3 MB
4096x768    58.1 us      0.22 us         12 MB
8192x768   142.7 us      0.43 us         24 MB

reshape! is host-side bookkeeping, so it stays under a microsecond whatever the array weighs. The reshape column is the copy, and it grows with the bytes.

The catch is that reshape! changes the array everything else is holding. It fits a temporary the calling expression owns, and not an argument, an ivar, or anything a cache still points at:

ruby
x = a * b            # a temporary nothing else holds
x.reshape!(t, n, h)  # free

Where the array is not yours to change, the copy is the price of the shape. Reach for reshape! when a profile says the copies are worth removing, not by default.

Ruby Floats In NMath Promote To Double

Cumo::NMath picks the module it dispatches to from every argument it is given, and a Ruby Float counts as a DFloat there. A single-precision array therefore comes back doubled whenever a plain Float rides along, even though the arithmetic operators leave it alone:

ruby
Cumo::NMath.atan2(a, 2.0)   #=> Cumo::DFloat
Cumo::NMath.atan2(a, b)     #=> Cumo::SFloat
a + 2.0                     #=> Cumo::SFloat

Numo promotes the same way, and on a CPU it costs nothing: Numo's single-precision math computes in double and narrows the result anyway. On a GeForce card, whose double-precision rate is a sixty-fourth of its single-precision one, it costs a great deal. 512x2048 elements in place on an RTX 5070 Ti Laptop:

                SFloat     DFloat
a * 2.0         11.8 us    12.8 us
sqrt            11.0 us    43.0 us
sin             11.5 us    97.5 us
atan            11.1 us   122.9 us
atan2           12.5 us   192.4 us

…

ruby
two = Cumo::SFloat[2.0][0]     # a 0-dimensional Cumo::SFloat
Cumo::NMath.atan2(a, two)      #=> Cumo::SFloat, 14.9 us against 219.3 us

Naming the module directly works too, under both libraries:

ruby
Cumo::SFloat::Math.atan2(a, 2.0)   #=> Cumo::SFloat

The 0-dimensional form has no effect under Numo, where [] returns a Ruby Float.

Fused Operations

layer_norm, rms_norm and softmax normalize along the last axis in one kernel each. Written out of the operators they take nine launches, six and five, and a launch costs about two microseconds whatever it is handed, so a short row pays for the launches rather than for its bytes.

ruby
y = x.layer_norm(gamma, beta, eps: 1e-5)   # (x - mean) / sqrt(var + eps) * gamma + beta
z = x.rms_norm(gamma, eps: 1e-5)           # x / sqrt(mean(x * x) + eps) * gamma
probs = scores.softmax                     # exp(x - max) / sum(exp(x - max))

On an RTX 5070 Ti Laptop, against the same arithmetic spelled with operators, in microseconds:

layer_norm              SFloat            DFloat            HFloat
shape              fused  written    fused  written    fused  written
1 x 768              3.9     30.1      9.7     30.3      3.9     29.8
256 x 768            6.6     22.5     22.0     35.4      7.8     22.4
4096 x 768          32.7    177.4    300.7    610.5     28.2    142.9
1 x 1000000         19.0     59.4    125.4    175.4     22.3     68.5

softmax                 SFloat            DFloat            HFloat
shape              fused  written    fused  written    fused  written
1 x 768              4.3     19.8      7.4     26.5      6.3     34.4
256 x 768            8.8     13.9     23.4     35.1      6.8     18.5
4096 x 768          49.2    122.7    318.3    549.0     28.2    109.8
1 x 1000000         27.8     44.0    108.6    148.5     32.1     53.8

rms_norm                SFloat            DFloat            HFloat
shape              fused  written    fused  written    fused  written
1 x 768              2.9     14.2      5.8     14.1      3.1     15.1
256 x 768            4.6     16.7      6.9     20.2      5.2     15.5
4096 x 768          38.5    122.5    130.8    367.0     19.2     80.8
1 x 1000000         12.5     35.8     23.7    102.2     12.0     33.2

…

ruby
x = Cumo::SFloat[[1.0, 2.0, 3.0, 4.0]]
ones = Cumo::SFloat.ones(4)
zeros = Cumo::SFloat.zeros(4)
x.rms_norm(ones)               #=> [[0.365, 0.730, 1.095, 1.461]]
x.layer_norm(ones, zeros)      #=> [[-1.342, -0.447, 0.447, 1.342]]

…

ruby
Cumo::NMath.silu(x)   # x / (1 + exp(-x)), in one kernel rather than five

Written out of the operators it costs five launches, and one kernel runs 1.1x to 4.3x faster on an RTX 5070 Ti Laptop, in microseconds:

silu                    SFloat            DFloat            HFloat
elements           fused  written    fused  written    fused  written
768                  3.6     13.4      3.6      9.5      3.4     14.3
786432               7.0     25.9     90.5    102.4      5.2     19.2
16777216           353.2   1312.9   1984.7   3258.8    199.6    851.8

…

ruby
Cumo::HFloat[2049.0]        #=> 2048.0
Cumo::HFloat[50257.0]       #=> 50272.0
Cumo::HFloat[100000]        #=> Infinity

There is no exception on the way past the top; the value saturates, as it does in every other float type. Anything that carries an index rather than a measurement has to be built in a wider type and cast afterwards.

Reductions do not inherit that limit, because they accumulate in single precision and round once at the end. A sum of forty thousand ones is forty thousand, not the 2048 a half accumulator would stop at:

ruby
Cumo::HFloat.new(40_000).fill(1.0).sum   #=> 40000.0

sum, mean, var, stddev, rms, mulsum, dot, gemm and cumsum all widen this way. prod and cumprod do not: a product leaves half's range long before it loses precision, so widening the accumulator would only hide the overflow.

The widening protects the accumulation, not the answer, which is still stored as half. A variance above 65504 therefore saturates even though nothing overflowed while it was being computed, and the standard deviation of the same array is fine because the square root brings it back into range:

ruby
a = Cumo::HFloat[2000.0, -2000.0, 1000.0, -1000.0]
a.var      #=> Infinity
a.stddev   #=> 1826.0

…

ruby
a = Cumo::HFloat.ones(1, 1024)
b = Cumo::HFloat.new(1024, 1).fill(100.0)
a.dot(b)                   #=> Infinity
a.gemm(b, alpha: 0.001)    #=> 102.375, the true 102400 scaled down

What half is faster at

gemm reaches the tensor cores. Square matrices on an RTX 5070 Ti Laptop, median of three runs each:

              HFloat              SFloat              DFloat
1024x1024     0.043 ms  49.7 TF   0.161 ms  13.3 TF   5.370 ms  0.40 TF
2048x2048     0.362 ms  47.4 TF   1.360 ms

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ccudanumorubyscicentific-computing

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类DevOps
定价开源

> 相关工具

D
Docker
容器化平台,标准化应用交付
G
GitHub Actions
GitHub 原生 CI/CD 工作流
N
Nginx
高性能 Web 服务器与反向代理