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 / use_var_in_struct.md
Last active November 15, 2024 00:18
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 October 2, 2024 22:07
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

@niw
niw / LongPressAndTapGesture.swift
Last active July 5, 2024 07:02
If you want to implement a view like a capture button on camera app.
/*
Copyright (c) 2024 Yoshimasa Niwa
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
commit 6ac1a2feb7143a8bc0e57af5c8dc57cbbb05d85f
Author: Yoshimasa Niwa <[email protected]>
Date: Fri Apr 19 22:14:55 2024 -0700
wip
diff --git a/3_bidirectional_cxx_interop/include/fibonacci/fibonacci.h b/3_bidirectional_cxx_interop/include/fibonacci/fibonacci.h
index 911e7d7..ed3f909 100644
--- a/3_bidirectional_cxx_interop/include/fibonacci/fibonacci.h
+++ b/3_bidirectional_cxx_interop/include/fibonacci/fibonacci.h
@niw
niw / main.swift
Last active November 29, 2023 05:28
Texting line height on NSTextView with TextKit 2
import AppKit
import Foundation
final class MainView: NSView {
private let textView: NSTextView
override init(frame frameRect: NSRect) {
textView = NSTextView(usingTextLayoutManager: true)
super.init(frame: frameRect)
@niw
niw / test.py
Last active August 27, 2023 13:26
Use CodeLlama on macOS with MPS quickly
# Usage
# =====
#
# ## Prerequisite
#
# Prepare Python 3, for exmaple, install Homebrew and `brew install python`.
#
# ## Install dependencies
#
# $ python3 -m venv .venv
@niw
niw / main.swift
Created July 13, 2023 06:29
Simple parser combinator, lexer, paresr and evaluator for double value math for simple logic configuration
import Foundation
extension String: Error {}
// MARK: - Parser Combinator
typealias ParserFunction<Element, Output> =
(any Collection<Element>) throws -> (Output, any Collection<Element>)
protocol Parser<Element, Output> {
@niw
niw / main.swift
Created July 12, 2023 04:15
Tiny SExpr Interpretor
import Foundation
extension String: Error {}
// MARK: - Evaluation
enum Atom {
case symbol(String)
case number(Double)
}