C.W.K.
Stream
Lesson 02 of 07 · published

Array, dtype, 그리고 unified-memory 의 차이

~14 min · arrays, dtypes, unified-memory

Level 0Curious
0 XP0/51 lessons0/15 achievements
0/100 XP to next level100 XP to go0% complete

mx.array 생성자

MLX array 는 unified memory 안의 typed N-차원 버퍼야. Python list (또는 NumPy array, 또는 다른 MLX array) 에서 만들고, shape, dtype, ndim 가져. NumPy 또는 PyTorch tensor 써봤다면, 다음 30 초 타이핑이 정확히 같게 느껴질 거야.

다르게 알아챌 부분은 작지만 architectural — 어디에도 device 인자 없고, 다음 레슨에도, 그 다음 레슨에도 없을 거야.

Dtype 셋은 ML-shape 이지, 과학 전반 아냐

NumPy 는 일반 목적 과학 라이브러리라 넓은 numeric type 셋 지원 — complex128, datetime64, structured record, 다 갖춰. MLX 의 dtype 셋은 ML 워크로드용으로 박혔어. mlx 0.31.x 기준 지원 dtype 은 float32 (대부분 기본), float16, bfloat16, int8 / int16 / int32 / int64, uint8 / uint16 / uint32 / uint64, bool, 그리고 complex 두어 개. Datetime 없음, structured record 없음, extended precision 없음.

그게 feature 지 limitation 이 아냐. 더 작은 표면은 MLX 안의 더 단순한 kernel 코드 의미하고, 그건 최적화할 path 가 더 적고 유지할 corner case 가 더 적다는 뜻. complex128 필요하면 아마 잘못된 도구 잡고 있는 거.

기본 device 는 unified memory, 항상

네가 만드는 모든 mx.array 는 기본으로 unified memory 에 살아. Apple Silicon 에서 mx.default_device()Device(gpu, 0) 돌려줘 — 근데 foundations.lesson2 에서 배운 대로, 그건 다음 op 을 어느 compute unit 이 돌릴지의 라벨이지, byte 가 어디 사는지 아냐. Byte 는 시작부터 끝까지 한 공유 풀에 살아.

Type promotion 이 조심할 한 곳

Op 에서 dtype 섞으면 (예 int32_array + float32_array), MLX 는 대부분 NumPy 와 일치하는 룰로 더 넓은 type 으로 promote — 다 그렇진 않지만. 안전한 습관, 특히 학습된 weight (항상 float) 와 integer index 섞을 때, 의도할 때 명시적으로 .astype(mx.float32) 호출. Implicit promotion 은 디버깅 한 시간 잃기 전까지 편리해.

Code

Array 만들기 — NumPy 에서 아는 같은 shape 들·python
import mlx.core as mx

a = mx.array([1, 2, 3, 4])
b = mx.array([1.5, 2.5, 3.5, 4.5])
c = mx.zeros((3, 4))
d = mx.ones((2, 2), dtype=mx.float16)

print('a:', a, 'dtype:', a.dtype, 'shape:', a.shape, 'size:', a.size, 'ndim:', a.ndim)
print('b:', b, 'dtype:', b.dtype)
print('c shape:', c.shape, 'dtype:', c.dtype)
print('d:', d, 'dtype:', d.dtype)

# Verified output (2026-05-03):
#   a: array([1, 2, 3, 4], dtype=int32) dtype: mlx.core.int32 shape: (4,) size: 4 ndim: 1
#   b: array([1.5, 2.5, 3.5, 4.5], dtype=float32) dtype: mlx.core.float32
#   c shape: (3, 4) dtype: mlx.core.float32
#   d: array([[1, 1], [1, 1]], dtype=float16) dtype: mlx.core.float16
Explicit dtype 변환 — .astype·python
import mlx.core as mx

a = mx.array([1, 2, 3, 4])               # inferred → int32
print('a       :', a, a.dtype)

a_f = a.astype(mx.float32)
print('a.astype:', a_f, a_f.dtype)        # → float32

# Verified:
#   a       : array([1, 2, 3, 4], dtype=int32) mlx.core.int32
#   a.astype: array([1, 2, 3, 4], dtype=float32) mlx.core.float32

# Be explicit when you mean float — implicit promotion to float32
# happens in arithmetic anyway, but .astype documents your intent.
기본 device 가 unified-memory 배치를 확인·python
import mlx.core as mx

# The default 'device' is just a label about where compute runs next.
# All bytes live in the unified-memory pool either way.
print('default device:', mx.default_device())   # → Device(gpu, 0) on Apple Silicon

x = mx.array([1.0, 2.0, 3.0])
print('x lives in :', mx.default_device(), '— but the bytes are in unified memory.')

External links

Exercise

네가 고른 다섯 array 만들기 — int32 하나, float32 하나, float16 하나, bool 하나, shape (3, 5) zeros 하나. 각각에 대해 dtype, shape, ndim, size 출력. 그 다음 둘 골라서 arithmetic op (+ 또는 *) 해 — 결과 dtype 이 뭐? 더 높은 정밀도 dtype 으로 promote 되는 조합 하나, 안 되는 거 하나 시도. 알아챈 거 두 문장.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.