Skip to content

Instantly share code, notes, and snippets.

View niw's full-sized avatar
🐱
Meow

Yoshimasa Niwa niw

🐱
Meow
View GitHub Profile
@niw
niw / UUIDv7.swift
Last active July 25, 2025 19:21
A simple UUIDv7 implementation in Swift.
//
// UUIDv7.swift
// UUIDv7
//
// Created by Yoshimasa Niwa on 7/25/25.
//
import Foundation
import Security
@niw
niw / SeverSentEvent.swift
Last active July 24, 2025 01:02
A simple Sever-Sent Event Client implementation on `URLSession`
import Foundation
public enum ChunkedDataTaskError: Swift.Error {
case notSuccess(_ data: Data, _ httpResponse: HTTPURLResponse)
}
private extension Int {
var isSuccess: Bool {
(200 ..< 300).contains(self)
@niw
niw / MainView.swift
Last active May 12, 2025 23:02
Sample code to implement a vertically gorwing NSTextView in SwiftUI
import AppKit
import SwiftUI
public struct TextView: NSViewRepresentable {
@Binding
var text: String
var font: NSFont?
public init(
text: Binding<String>
@niw
niw / Observation.swift
Created March 25, 2025 01:26
Experimental `onChange(of:initial:perform:)`
import Foundation
import Observation
func onChange<Value>(
of value: @autoclosure () -> Value,
initial: Bool = false,
perform: (_ oldValue: Value, _ newValue: Value) -> Void,
_: isolated Actor = #isolation
) async throws {
let (stream, continuation) = AsyncThrowingStream.makeStream(of: Void.self)
@niw
niw / colors.sh
Created December 31, 2024 14:18
Prints each color in the palette on terminal emulator.
#!/usr/bin/env bash
for i in {0..255}; do
printf "\x1b[48;5;%sm%3d\e[0m " "$i" "$i"
if (( i == 15 )) || (( i > 15 )) && (( (i-15) % 6 == 0 )); then
printf "\n"
fi
done
@niw
niw / comfyui_mochi_vae_on_mps.diff
Last active December 6, 2024 03:36
A quick patch to make Mochi VAE works on macOS
diff --git a/comfy/ldm/genmo/vae/model.py b/comfy/ldm/genmo/vae/model.py
index b68d48a..694b33a 100644
--- a/comfy/ldm/genmo/vae/model.py
+++ b/comfy/ldm/genmo/vae/model.py
@@ -78,7 +78,12 @@ class PConv3d(ops.Conv3d):
# Apply padding.
assert self.padding_mode == "replicate" # DEBUG
mode = "constant" if self.padding_mode == "zeros" else self.padding_mode
+ is_mps_device = x.device.type == "mps"
+ if is_mps_device:
@niw
niw / use_var_in_struct.md
Last active June 13, 2025 17:21
Use `var` in `struct` in Swift

Use var in struct in Swift

TL;DR: Use var for properties in struct as long as it serves as a nominal tuple. In most cases, there is no obvious benefit to using let for struct properties.

Simple Example

Let's start with a simple example:

struct MyStruct {
@niw
niw / check.swift
Last active November 5, 2024 10:34
Check `MPSGraph.padTensor(_:with:leftPadding:rightPadding:constantValue:name)` behavior.
#!/usr/bin/env xcrun swift
import Foundation
import MetalPerformanceShaders
import MetalPerformanceShadersGraph
let device = MTLCreateSystemDefaultDevice()!
//let size = 256 * 256 - 1 // This size should work
let size = 256 * 256 // This size may not work
@niw
niw / update_install_names.rb
Created October 11, 2024 13:57
Make frameworks portable.
#!/usr/bin/env ruby
require 'optparse'
require 'pathname'
def update_name(name, options)
pattern = /#{options[:rpath]}/
if name =~ pattern
suffix = $'
if options[:path].join(suffix).exist?
@niw
niw / 20240929.md
Last active January 23, 2025 20:25
The Hunt for the iOS 18 Neural Engine Bug: A Long Weekend Investigation

The Hunt for the iOS 18 Neural Engine Bug: A Long Weekend Investigation

This is the tale of a long weekend spent uncovering a mysterious iOS 18 Neural Engine bug—a journey of problem-solving in a system where full visibility is elusive, especially in the locked-down world of Apple’s platforms. But the process I followed is a general approach you can use for any opaque system. It all began last week when I stumbled upon a strange behavior in my iOS app. The output generated from a CoreML model was completely broken—something I had never seen before. And after some digging, I realized this only happened when the model was running on the Neural Engine of iOS 18. The first step was triage. I implemented a quick workaround in the app: if the device is running iOS 18, switch from the Neural Engine to the GPU. This temporarily solved the issue, but I had no idea why it worked or whether other CoreML models in the app’s pipeline might also be affected. Without a deeper understanding of the root cause, I knew I cou